2

So another question led me to wondering if there is a way to negate a regex backreference without using lookarounds? The original post is here, but the approved answer is using lookarounds. In places those are not supported, is there a clean way to handle negative backreferences? I have found on a regex site [^\x] where x is the backref. number, does not work as intended.

For example: Find a number followed directly by any other number (but dynamic in the number). It would make sense to have (\d)[^\1], but inside a character class, everything is taken literally.

Community
  • 1
  • 1
Walls
  • 3,832
  • 6
  • 32
  • 50
  • 2
    Short answer: no. The real question is: why don't you want to use a lookahead? – HamZa Apr 17 '14 at 20:43
  • @HamZa it is more of a theory question and not something I need to actually apply. In theory, how are the lookarounds being generated by the regex engine? If they are (I am not sure if this is how they work) are just more complex rules all tied together in a simple formatting, then the engine can generate them and knows the rules behind them to represent without using the shorthand. – Walls Apr 17 '14 at 20:45
  • Not sure what you mean by "generated". Anyways, say for example we have `(\d)(?!\1)`. The regex engine will match a digit and put it in `group 1`, then it will check if there is no `\1`. It's a backreference so it will look what was matched in `group 1`. In this case a certain digit. Now if you really want to see another trick, here's one that only works in PCRE/perl `(\d)\1(*SKIP)(*FAIL)|\d`. See the [demo](http://regex101.com/r/oD0oH9) which has some explanation. Now honestly, what do you prefer? This weird skip fail regex or a negative lookahead? – HamZa Apr 17 '14 at 20:50
  • 1
    An other way with perl/pcre, using an atomic group followed by a conditional with an always false assertion: `(\d)(?>(\1)|\d)(?(2)\A)` – Casimir et Hippolyte Apr 17 '14 at 21:25
  • 2
    You can do the same with java with a possessive quantifier and an impossible number of repetition (depending of the context): `(\\d)(\\1?+)\\d\\2{1000}` – Casimir et Hippolyte Apr 17 '14 at 21:50
  • 1
    @Casimiret, nice trick. Even tho such inputs are not impossible. ;) (If OP was considering the possibility to get negative lookaheads where there are none (I got that impression for some reason): I don't know of any regex flavors that support possessive quantifiers/atomic groups that do not support negative lookaheads, while there are several with the other way around.) – Qtax Apr 17 '14 at 22:28
  • @Qtax: You are right, this is the reason I didn't write an affirmative answer to this question. – Casimir et Hippolyte Apr 17 '14 at 22:32

1 Answers1

4

is a way to negate a regex backreference without using lookarounds?

No.

In theory, how are the lookarounds being generated by the regex engine?

Lookarounds are not "generated" with other simpler regex constructs, they are a distinct feature of their own.

Qtax
  • 31,392
  • 7
  • 73
  • 111