regexstringmatching

how to use regular expression to match strings followed by some keyword and multiple lines


I have a string like this:

...
xxxx
xxx

keyword1 xxxxx
xxxx
xxxx
xxxxx keyword2 yyyy
xxx
xxxx

xxx
...

where the x and y's are just random chars. I need to match the first "keyword2 yyyy" that appears after keyword1, and there may be multiple lines after keyword1. How do I write the regular expression? Thanks!


Solution

  • You can use this regex,

    ^(?s).*keyword1.*?(keyword2 yyyy).*$
    

    Explanation:

    Demo