2

I try to find any string it not exactly one or more word

My pattern

(?!(^ignoreme$)|(^ignoreme2$))

Iam looking for

ignoreme   - no
ignoreme2  - no
ignoremex  - match
ignorem    - match
gnoreme    - match
ignoreme22 - match

But it return many space. How to do that thank. https://regex101.com/r/u4EsNv/1

DeLe
  • 2,442
  • 19
  • 88
  • 133

1 Answers1

6

You may use this corrected regex:

^(?!ignoreme2?$).*$

Updated RegEx Demo

RegEx Details:

  • ^: Start
  • (?!ignoreme2?$): Negartive lookahead to fail the match when we have ignoreme or ignoreme2 ahead till end.
  • .*: Match 0 more of any characters
  • $: End

Note that regex (?!(^ignoreme$)|(^ignoreme2$)) matches first 2 invalid cases because you have included ^ in negative lookahead expressions not outside. This causes regex engine to start matching after 1st character to satisfy lookahead assertions. (You can see that in regex101 highlighted matches)

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    it look cool, but it not separation, b/c i can add many word like `ignoreme2, ignoreme, top, abc` – DeLe Jan 22 '19 at 15:16
  • 1
    Sure you can add them with alternation e.g. `^(?!(ignoreme2?|top|abc|foo|bar)$).*$` – anubhava Jan 22 '19 at 15:17