0

I simply don't understand what the \G anchor does.

If I execute /\G\d\d/ on 1122aa33, it will match 11 and 22. However, when I try /\d\d\G/ on 1122aa33, it matches nothing.

Can someone enlighten me?

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
slier
  • 6,511
  • 6
  • 36
  • 55

3 Answers3

2

\G is an anchor which matches the previous match position.

On the first pass, \G is equivalent to \A, which is the start of the string anchor. Since \d\d\A will never match anything (because how can you have two digits before the start of the string?), \d\d\G will also never match anything.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
  • so that mean \G only use to check with previous match?.. so \d\d\G is pure invalid syntax right? – slier Aug 06 '10 at 21:18
  • 4
    It's not invalid, per se, but it is meaningless. Basically, the regular expression engine continues evaluating your string after a match is found, either to see if that match can be expanded (ie. `/.*/` with the string 'abc' will first match `a`, then expand the match to `ab`, then to `abc`) or if any other matches can be found. In the latter case, `\G` specifies that subsequent matches must begin following the previous match. – Daniel Vandersluis Aug 06 '10 at 21:23
  • yes, you worded it (much) better: it's indeed **not** invalid. – Bart Kiers Aug 06 '10 at 21:28
  • @DanielVandersluis i am sorry for unburying this topic again but the meaningless part is not entirely clear in my mind. \G matches the beginning of the string or the end of the last match (Java) so, when i use \Gdog on "dog dog", i only get the first dog. When i put dog\G, it does not match at first because there are no characters before the line start, right? Therefore, there can't be no more matches from then on because there isn't a first one (non-continuous matching). Also, since the engine moves forward after one match, dog\G wouldn't make sense since there is no back matching (i assume). – BrunoMCBraga Jun 18 '15 at 10:25
1

Reference link

It basically matches from the end of the "previous match", which on the first run of a regex is considered to be the beginning of the string.

In other words, if you ran /\G\d\d/ twice on your string 1122aa33, the second run would return a match on 22.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
1

According to this:

The anchor \G matches at the position where the previous match ended. During the first match attempt, \G matches at the start of the string in the way \A does.

Now, to actually answer your question: In your second example, the one that yields no results, \G can't match the start of the string, because you're seeking two digits first, and, without that initial match, \G won't match anything else, either.

Dan J
  • 16,319
  • 7
  • 50
  • 82