in reply to anchor ^ and \G
After the first match, \G will no longer be matching at the start of the string (^), so the output will just be
whitespace done
Both ^ and \G are called "assertions". The Assertions section of perlre has quite a bit to say about \G and provides links to further information.
It is certainly possible to use both of those assertions in the same regular expression. In fact, changing each instance of '^\G' in your code to '^.*\G', provides your desired output.
Here's my test code:
#!/usr/bin/env perl use strict; use warnings; my $string = " a 1 # "; my $i = 0; while () { if ( $string =~ /^.*\G\s+/gc ) { print "whitespace\n"; } elsif ( $string =~ /^.*\G[0-9]+/gc ) { print "integer\n"; } elsif ( $string =~ /^.*\G\w+/gc ) { print "word\n"; } else { print "done\n"; last; } }
Output:
whitespace word whitespace integer whitespace done
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: anchor ^ and \G
by Anonymous Monk on Jun 28, 2015 at 00:00 UTC |