0

I've tried this

Regex:

^(?:(?![_]{2}).)+$

Text:

abcc..abc

abcdefgh_

abcd_abcd

abc_a_abc

abc__abcd

a__abcde_

abc++abcd

where highlight texts mean match.

The regex above is literally mean "match any line that constructed from any characters except __", I just wonder about evaluation process of this regex, in my understanding the process suppose to work like this

Consider text: abc__abcd

Evaluation process:

  1. check condition ^ (match).
  2. check condition .+ (match).
  3. check condition $ (match).

So far, all regex still can match to our considered text.

  1. check condition of negative lookahead.

(?![_]{2})a(?![_]{2})b(?![_]{2})c(?![_]{2})_(?![_]{2})_(?![_]{2})a(?![_]{2})b(?![_]{2})c(?![_]{2})d

negative lookahead executing:

(?![_]{2}) ab (match)
(?![_]{2}) bc (match)
...
(?![_]{2})__ (not match)
...

Is my understanding correct?

fronthem
  • 3,617
  • 5
  • 26
  • 49
  • No, my question is about order of execution between pattern expansion and lookaround. – fronthem Aug 24 '15 at 10:31
  • You did not expand the tempered greedy token, you just created some new one as the original one matches any character other than a newline that is not starting `__` sequence. You have specific characters specified. Note that the original regex consumes character one by one, not by pairs. – Wiktor Stribiżew Aug 24 '15 at 10:34
  • @stribizhev Sorry, I don't understand, I have not much deep knowledge about execution process in regex, If you know how my regex actually work, please answer below step by step. – fronthem Aug 24 '15 at 10:38
  • Have a look at the original question answer, I think it is explained well there. – Wiktor Stribiżew Aug 24 '15 at 10:40
  • 2
    Also, a [Lookahead and Lookbehind Zero-Length Assertions](http://www.regular-expressions.info/lookaround.html) is a must read here. When a lookahead is checking for consecutive `__`, the regex index is standing still where it was: right before each symbol. It checks and returns true or false before going forward. – Wiktor Stribiżew Aug 24 '15 at 10:52
  • you can put your regex in [this website](https://regex101.com/), it can debug regex match step. – Kerwin Aug 24 '15 at 11:00
  • @stribizhev Thank you a lot for your advice, It's indeed improve my knowledge. May I ask you one question? Can we categorize element in the regex into two types which are 1) pattern 2) validator (lookaround)? because I think, in theory lookaround is not regex but it's just a feature of validation. – fronthem Aug 24 '15 at 11:38
  • 3
    A look-around is also a pattern. Validation is a domain where regular expressions can be used. – Wiktor Stribiżew Aug 24 '15 at 11:43

0 Answers0