-2

Lets take:

stringi = 'xnxx xnnx xnnxn'

My regex is: (n)[^n]

I want to make my regex a little more dynamic like that:

(n)[^\1] -\1 beeing the capt. grp 1

My desired result would be that:

  • (n)[^\1] would be equal (n)[^n]
  • (x)[^\1] would be equal (x)[^x]

How can I not match a NOT-\1 character?

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69

1 Answers1

1

using a negative lookahead, the . is to match any character as n length is one

(n)(?!\1).
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • absolutely getting your idea. Very smart. Yet i don't get the same result as `(n)[^n]` on regex101.com. – Andre Elrico Mar 19 '18 at 12:48
  • I think I know why. The `.` does not consume new lines. Your solution is absolutely correct. Yet I know how I could tweak it. `(n)(?!\1)(?:.|[[:space:]])` – Andre Elrico Mar 19 '18 at 12:52
  • 1
    exactly `.` doesn't match newline by default but can be changed using flag `/s`, or depending engine `(?s).` – Nahuel Fouilleul Mar 19 '18 at 12:52