Home > Archive > PERL Beginners > January 2006 > What is so "meta" about @ character in Regex?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What is so "meta" about @ character in Regex?
|
|
|
| According to FAQ:
"The Perl parser will expand $variable and @variable references
in regular expressions unless the delimiter is a single quote".
"Expand $variable" is well documented, but what does "expand
@variable references" mean and how does it work?
I keep thinking array here and fail to see how an multi-
element array is interpolated and used inside a pattern.
Any explanation is appreciated!
| |
| Paul Lalli 2006-01-22, 7:03 pm |
| MSG wrote:
> According to FAQ:
> "The Perl parser will expand $variable and @variable references
> in regular expressions unless the delimiter is a single quote".
>
> "Expand $variable" is well documented, but what does "expand
> @variable references" mean and how does it work?
> I keep thinking array here and fail to see how an multi-
> element array is interpolated and used inside a pattern.
A pattern match goes through several levels of parsing:
1) The code it parsed to determine that there is a pattern match (ie,
perl looks for the m// itself).
2) The contents of the pattern match are interpreted as a double quoted
string, interpolating all variables and backslash-escapes
3) The resulting string is passed to the regular expression parser.
It is step 2 that called into question here. $variable and @variable
are both interpolated just as they would in any other double quoted
string before the regular expression parser is ever invoked.
Ex:
#!/usr/bin/env perl
use strict;
use warnings;
my @vars = ('foo', '(\w+)', 'bar');
my $string = "string with foo and bar in it";
if ($string =~ /^string.*@vars/) {
print "Matched in parens: '$1'\n";
} else {
print "No match\n";
}
__END__
Matched in parens: 'and'
This happened because after step 2 above, the regular expression engine
actually saw:
/^string.*foo (\w+) bar/
because the array @vars was interpolated just as it would be in a
double quoted string - the elements were separated by a space
character.
Does this help explain what you were asking about?
Paul Lalli
| |
|
|
Paul Lalli wrote:
> MSG wrote:
>
> A pattern match goes through several levels of parsing:
> 1) The code it parsed to determine that there is a pattern match (ie,
> perl looks for the m// itself).
> 2) The contents of the pattern match are interpreted as a double quoted
> string, interpolating all variables and backslash-escapes
> 3) The resulting string is passed to the regular expression parser.
>
> It is step 2 that called into question here. $variable and @variable
> are both interpolated just as they would in any other double quoted
> string before the regular expression parser is ever invoked.
>
> Ex:
> #!/usr/bin/env perl
> use strict;
> use warnings;
>
> my @vars = ('foo', '(\w+)', 'bar');
> my $string = "string with foo and bar in it";
>
> if ($string =~ /^string.*@vars/) {
> print "Matched in parens: '$1'\n";
> } else {
> print "No match\n";
> }
> __END__
> Matched in parens: 'and'
>
> This happened because after step 2 above, the regular expression engine
> actually saw:
> /^string.*foo (\w+) bar/
> because the array @vars was interpolated just as it would be in a
> double quoted string - the elements were separated by a space
> character.
>
> Does this help explain what you were asking about?
>
> Paul Lalli
Your explanation is excellent! Thanks Paul!
|
|
|
|
|