in reply to anchor ^ and \G
It's certainly possible to use both \G and ^ in a regular expression, but I expect that would be a very unusual use case. Both are markers telling the regex a reference point in a string. I've never needed to use both in a regex before, but you can certainly do it. Here's a contrived example:
$ cat gah.pl use strict; use warnings; my $t = "The quick red fox jumped over the lazy brown dog."; if ($t =~ /(jump)/g) { print "Found $1 ending at ", pos($t), ".\n"; } if ($t =~ /(\w+)\s+(\w+)\G/) { print "Found <$1> before <$2>\n"; } $t = <<EOTxt; Now a funkier thing we can do is to find weird stuffs such as the text before a word on the same line EOTxt if ($t =~ /(weird)/g) { print "Found $1\n"; if ($t =~ /^([^\n]+)\G/gm) { print "<$1>\n"; } } $ perl gah.pl Found jump ending at 22. Found <fox> before <jump> Found weird <do is to find weird>
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: anchor ^ and \G
by ikegami (Patriarch) on Jun 30, 2015 at 19:37 UTC |