0

Why the regex below not working for the text below

Regex:

(?=.*\berror\b)(?=.*\balarm\b).*

Text :

ns1.alarm.abc 
ns2.error.cdb

In other works how to do multiline regex match?

Ibrahim
  • 5,333
  • 2
  • 32
  • 48
Iamtheroot
  • 45
  • 4

1 Answers1

0

I think you are trying to find the strings containing either error or alarm and for that you can try the following:

Try this Regex:

(?=.*\b(?:error|alarm)\b).*

Click for Demo

Explanation:

  • (?=.*\b(?:error|alarm)\b) - Positive lookahead for checking the presence of the words - error OR alarm
  • .* - if the above condition is satisfied, match 0+ occurrences of all characters(except a newline character)

Your regex (?=.*\berror\b)(?=.*\balarm\b).* does not work as it is trying to find both the words error and alarm in the same line/input string. So, you needed to put an OR condition as shown above.

Update:

To match both, use this.

(?=[\s\S]*\berror\b)(?=[\s\S]*\balarm\b)[\s\S]* as shown HERE

Gurmanjot Singh
  • 8,936
  • 2
  • 17
  • 37