Search code examples
regexpcre

Multiline PCRE, multiple conditions


just starting out with regex and have hit a stumbling block. Hoping someone might be able to explain the workaround.

Trying to carry out a multi-line search. I wish to use "*" as the 'flag', so to speak: if a line contains an asterisk it should match. The digits at the start of the line should be output, so should the word "Match" in the linked example, excluding the asterisk itself.

I assume my use of "|" is dividing the regex into two conditions, when it actually needs to satisfy both to match.

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

(?m)(^\d+)|(?<=\*).*$

Any help kindly appreciated.


Solution

  • You could use a pos. lookahead as in

    ^(?=.*?\*)(\d+).+?(Match)$
    

    See your modified example on regex101.com.