# Explanation of G Modifier - PERL Beginners

Hi everyone, Sorry for asking this question again for what must be the umpteenth time but I have a problem with the g modifier when matching. I have read Newsgroup Postings about this topic including: "What good is \G in a regular expression?" Listed below is what I'm experiencing problems with: \$P{"letter"} = "([A-Z])"; \$P{"number"} = '(\d{1,2})'; \$src = "a 1 b 2 c 3"; if(\$src =~ m/\$P{"number"}/sgi) { \$num = \$1; } if(\$src =~ m/\$P{"letter"}/sgi) { \$let = \$1; } print "Number: \$num\n"; #-->(Prints "1") print "Letter: \$let\n";#-->(Prints "b") #(Prints "a") <-> If Letter Matching Comes First then Number ...

1. ## Explanation of G Modifier

Hi everyone,
Sorry for asking this question again for what must be the umpteenth
time but I have a problem with
this topic including:
"What good is \G in a regular expression?"

Listed below is what I'm experiencing problems with:

\$P{"letter"} = "([A-Z])";
\$P{"number"} = '(\d{1,2})';

\$src = "a 1 b 2 c 3";

if(\$src =~ m/\$P{"number"}/sgi)
{
\$num = \$1;
}

if(\$src =~ m/\$P{"letter"}/sgi)
{
\$let = \$1;
}

print "Number: \$num\n"; #-->(Prints "1")
print "Letter: \$let\n";#-->(Prints "b")
#(Prints "a") <-> If Letter Matching Comes First then Number Matching
# OR
#(Prints "a") <-> Without g Modifier

Based on FAQ 6.19, I'm trying to understand why Precedence matters when
using the g Modifier when matching
part of a string, even when that string hasn't been modified.
I don't have any problems when using the g Modifier in a Loop, for some
reason.

From FAQ 6.19:

-gohaku

Gohaku Guest

2. ## Re: Explanation of G Modifier

gohaku wrote:

G is likely best for s/// syntax as there is a
better (?:test) for pattern matching in
general:

\$_ = "I match it All, so you dont have to";

s%a%_%ig;

print "\$_";

you get
I m_tch it _ll, so you dont h_ve to

G just causes the match to match everywhere
the pattern is to be found. But NOT beyond
the end of the line (\n) which is a logical
record separator...

HTH;
-Sx-
Wc Guest

3. ## Re: Explanation of G Modifier

gohaku wrote:

\$_ = "a 1 b 2 c 3";

print "I see numbers" if /(?!\d)/;

__END__

Or /(?!\w)/ et al...

Maybe /pattern/sgi isn't what you want for your test?

-Sx-
Wc Guest

4. ## Re: Explanation of G Modifier

WC -Sx- Jones wrote:
>
>
>
> G is likely best for s/// syntax as there is a
> better (?:test) for pattern matching in
> general:
>
> \$_ = "I match it All, so you dont have to";
>
> s%a%_%ig;[/ref]

Always uses slashes unless the regex contains multiple slashes or
backslashes. When you do have to use a different regex quote character,
please pick one that is not so noisy.

The \G modifier causes the match to begin at the position of the last
match. Ex.

while ('a 1 b 2 c 3' =~ /\G\D+(\d)/g) { print \$1 }

=> 123

1st iteration:
'a 1 b 2 c 3'
p ^
2nd iteration
'a 1 b 2 c 3'
p ^
3rd iteration
'a 1 b 2 c 3'
p ^

Where p marks the pos where the search begins, which is the position of
the last match. And ^ marks the captured match.

See 'perldoc perlre'

Randy.
Randy Guest

5. ## Re: Explanation of G Modifier

WC -Sx- Jones wrote:
>
>
>
> G is likely best for s/// syntax as there is a
> better (?:test) for pattern matching in
> general:
>
> \$_ = "I match it All, so you dont have to";
>
> s%a%_%ig;[/ref]

Always uses slashes unless the regex contains multiple slashes or
backslashes. When you do have to use a different regex quote character,
please pick one that is not so noisy.

The \G modifier causes the match to begin at the position of the last
match. Ex.

while ('a 1 b 2 c 3' =~ /\G\D+(\d)/g) { print \$1 }

=> 123

1st iteration:
'a 1 b 2 c 3'
p ^
2nd iteration
'a 1 b 2 c 3'
p ^
3rd iteration
'a 1 b 2 c 3'
p ^

Where p marks the pos where the search begins, which is the position of
the last match. And ^ marks the captured match.

See 'perldoc perlre'

Randy.

Randy Guest

6. ## Re: Explanation of G Modifier

>> gohaku wrote: [/ref][/ref]

while (<>) {
chomp;
PR: {
m/ \G( \d+\b )/gcx && do { print "number: \$1\n"; redo; };
m/ \G( \w+ )/gcx && do { print "word: \$1\n"; redo; };
m/ \G( \s+ )/gcx && do { print "space: \$1\n"; redo; };
m/ \G( [^\w\d]+ )/gcx && do { print "other: \$1\n"; redo; };
}
}

Perl6 Faq ?
-Sx-
Wc Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•