如何使用正则表达式选择具有两个或更多条件组/匹配项的多行?

时间:2019-04-05 09:55:46

标签: regex regex-greedy

我需要用这种文件搜索[文档],然后在找到时选择第一行中的文本。如果下一行以空格开头,则三个点和空格也会从所有此类行中选择文本。

@{TEST_SCREENS_WITH_TIMEOUT}    ShowDesc    ShowOsAndCountryCode    ShowSW  
...                             Checksum_PPSU   Checksum_BPSU                   
...                             Datetime_Clock  Datetime_DDMM
...                             ShowHWRevBaseboard  ShowHWRevSOM

*** Keywords ***
Program Version contain
[Documentation]     Read out program version and compare it with
    ...                 Comparison is done with "Should Contain"
    ...                 it checks if 

Tarif EPS is
[Documentation]     Read out tarif EPS and compare it with ${TARIF_EPS}
    ...                 Comparison is done with "Should be equal"
[Arguments]         ${TARIF_EPS}

Should be equal    ${disp_traf_eps}    ${TARIF_EPS}    Tarif EPS


Release Date contain
[Documentation] Read out program version and compare it with ${RELEASE_DATE}

这是我到目前为止设法做到的:
    ^ \ s * [文档] \ s +(。+)(?:(?= \ s + ... \ s +(。+)))*

到目前为止,我尝试过的整个文本和正则表达式都可以在这里找到:https://regex101.com/r/Jbzd4e/1

在我的情况下,它仅选择2行。我只需要选择文档文本!

1 个答案:

答案 0 :(得分:1)

怎么样:^\s{1,}\[Documentation]\s*(.*)$|^\s{1,}\.{3}\s*(.*)$

https://regex101.com/r/C5FTzP/2

更新;

或者这可能更优雅:^\s{1,}(\[Documentation]|\.{3})\s*(.*)$

https://regex101.com/r/C5FTzP/4