0

The question is about lookahead and lookbehind assertion.

Reference to lookahead:

(?=...)

Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.

Match Issac and look its behind, but called "lookahead".

While:

(?<=...)

Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in 'abcdef', since the lookbehind will back up 3 characters and check if the contained pattern matches.

Match def and check its head for abc but called "lookbehind".

I am very confused about the unintuitive name of lookbehind and lookahead. Where do the names originate?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 3
    It's pretty clear for me, it matches `Issac ` and the lookahead make sure there is `Assimov` ahead, it's well named `lookahead`. I don't understand your question. – Toto Nov 30 '19 at 10:24
  • 2
    Funny question. To me it's very intuitive that the *lookahead* assertion looks ahead for `Asimov` and the *lookbehind* looks behind for `abc` :) – bobble bubble Nov 30 '19 at 10:24
  • 2
    I think it's more of a "forward" / "backward" interpretation rather than "after" / "before". – revo Nov 30 '19 at 10:25
  • 4
    I'm voting to close this question as off-topic because it is not about programming. – Toto Nov 30 '19 at 10:25
  • After reading @revos comment I'm better understanding your question. Seems like for your understanding e.g. in `abc` the `c` is *behind* the `b` and the `a` *ahead* of the `b`? Just imagine as the parser proceeds in the string, characters to the left, that are passed are *behind* and character to the right are *ahead*. – bobble bubble Nov 30 '19 at 10:32
  • Since the target is "issac", so we lookarond Issarc. focus on the target, focus on the pattern. I'm looking for the target, so I first place my eyes on the target. the names of lookahead or lookhehind is anti-intuitive but it silly easy to understand just remember anti-intuitive, my question where does it origniate? – AbstProcDo Nov 30 '19 at 10:37

1 Answers1

3

enter image description here

While Rex walks from start to end of the string, some things are ahead and others behind.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46