-1

I am trying to find the inverse of the following string but with no success:

(?i)(?s)^(?=.*?word1)(?=.*?word2)

I built this but it is for one word only and it does not even function properly, considering that if I test it with regex101 I got one match while I should get no match:

(?i)(?s)(?!.*?word2)^.*$

Please see the following link: https://regex101.com/r/qS7yN9/72

Hope you guys can help to build the right string.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Radeon89
  • 17
  • 7
  • Which programming language or library are you using? – melpomene May 18 '19 at 20:58
  • Your pattern is correct, remove the default m modifier in regex101 that matches `^` as the start of a line. You can also replace `^` with `\A` to be sure to match the start of the string whatever the modifier. – Casimir et Hippolyte May 18 '19 at 21:02
  • @melpomene PCRE (PHP) according to regex101... – Radeon89 May 19 '19 at 07:51
  • @casimir please check if the string modified as follows is what you meant: https://regex101.com/r/qS7yN9/75 if so, please note that with the string modified like this, it searches for the "word1" only, ignoring the words that contain "word1". I would like words like for instance word12345 to be taken into consideration. Moreover, just in case my original post was not clear, the string should exclude text containing two words and only if both are present. Thanks! – Radeon89 May 19 '19 at 08:16
  • What do you mean, "according to regex101"? Do you just want to make colorful patterns appear on some website, or are you trying to solve a programming problem? – melpomene May 19 '19 at 13:54
  • @melpomene I need to insert that regex string inside Tasker, that is an Android app...so I would say the second one you said – Radeon89 May 19 '19 at 19:20
  • Tasker [does not use PCRE](https://tasker.joaoapps.com/userguide/en/matching.html#regex) (or PHP, for that matter). It uses [Java regexes](https://developer.android.com/reference/java/util/regex/Pattern.html). – melpomene May 19 '19 at 19:22
  • You may use `(?is)^(?!.*?(?:word1|word2)).*`, see [this regex demo](https://regex101.com/r/45Htdi/1) – Wiktor Stribiżew May 23 '19 at 22:49

1 Answers1

0

You can effectively negate the result by wrapping a negative look-ahead assertion around the whole thing:

(?i)(?s)^(?!(?=.*?word1)(?=.*?word2))
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • I tested it on regex101 but even both words are contained into the text it still get matches... please give a look here: https://regex101.com/r/qS7yN9/76 – Radeon89 May 20 '19 at 19:08