-2

If I get it right then a positive look ahead ( ?= ) as well a non-capturing group ( ?: ) are used for to excluse a part of the string in the final match-results.

What is the difference between the a lookahead and a non-capturing group?

Can anyone explain? Preferable with an easy understandable example?

mewi
  • 539
  • 1
  • 4
  • 11
  • 1
    They both don't exclude a part. A lookahead just looks forward to match/fail a pattern, but afterwards the cursor is still in the same position. A non-capturing group matches normally, but doesn't store it's match into a group that you could later refer to. – Sebastian Proske Sep 13 '16 at 11:42

1 Answers1

0

A positive look ahead is a zero width assertion. This means that it will not consume an input, e.g. in

/(?=.)./

the (?=.) part will match with the first characters of the string and the . will match with the same character. On the other hand

/(?:.)./

the (?:.) part will match with the first characters of the string (just like before) but the . will match with the second character because the first character will have been consumed by the parentheses.

redneb
  • 21,794
  • 6
  • 42
  • 54