Code Comments
Programming Forum and web based access to our favorite programming groups.I regularly use something along the lines of:
my $string = 'some string to match';
my @match = '';
if ( @match = $string =~ /(pattern)/ ) {
print "The match is\n@match\n";
}
else {
print "Sorry, Dave. No match.\n";
}
to test a regex by printing what matches. However, if the regex becomes
unwieldly and I assign it to a variable:
my $regex = qr{pattern};
my $string = 'some string';
my @match = '';
print "@match = $string =~ $regex\n"
I get a '1' returned instead of the matched bits.
If someone can let me know how to rewrite the above, I'd be grateful.
Thanks.
Post Follow-up to this messageOn Sat, 25 Sep 2004 12:27:22 GMT, Dave <dave@home.net> wrote:
>I regularly use something along the lines of:
>
> my $string = 'some string to match';
> my @match = '';
> if ( @match = $string =~ /(pattern)/ ) {
> print "The match is\n@match\n";
> }
> else {
> print "Sorry, Dave. No match.\n";
> }
>
>to test a regex by printing what matches. However, if the regex becomes
>unwieldly and I assign it to a variable:
>
> my $regex = qr{pattern};
> my $string = 'some string';
> my @match = '';
> print "@match = $string =~ $regex\n"
>
>I get a '1' returned instead of the matched bits.
What you've posted can't possibly be what you're using--Perl doesn't
interpolate arbitrary expressions in double quotes, at least not that
way. Assuming you meant
@match = $string =~ $regex;
print "@match\n";
the most likely explanation is that the pattern doesn't have any
parentheses in it. Using qr// won't magically parenthesize anything. If
all you want is what the entire pattern matches, you could use
@match = $string =~ /$regex/g;
--
Eric Amick
Columbia, MD
Post Follow-up to this messageDave wrote:
> I regularly use something along the lines of:
>
> my $string = 'some string to match';
> my @match = '';
> if ( @match = $string =~ /(pattern)/ ) {
> print "The match is\n@match\n";
> }
> else {
> print "Sorry, Dave. No match.\n";
> }
>
> to test a regex by printing what matches. However, if the regex
> becomes unwieldly and I assign it to a variable:
>
> my $regex = qr{pattern};
<buggy code snipped>
> I get a '1' returned instead of the matched bits.
>
> If someone can let me know how to rewrite the above, I'd be
> grateful.
Use capturing parentheses, either in the qr// operator, or do:
@match = $string =~ /($regex)/
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Post Follow-up to this messageDave <dave@home.net> wrote in comp.lang.perl.misc:
> I regularly use something along the lines of:
>
> my $string = 'some string to match';
> my @match = '';
That initialization is unnecessary and wrong. It is unnecessary because
in the next statement you assign to @match again. It is wrong because
it sets the first array element to an empty string. That is not a state
that would make any sense if there was no other assignment following.
> if ( @match = $string =~ /(pattern)/ ) {
> print "The match is\n@match\n";
> }
> else {
> print "Sorry, Dave. No match.\n";
> }
As long as there is only a single string to be captured, a scalar
variable $match would be clearer:
if ( my ( $match) = $string =~ /(pattern)/ ) {
print "The match is\n$match\n";
} # etc...
That would be for public(able) code. For use as a debugging aid I'd
stick with the array, it works in more cases.
> to test a regex by printing what matches. However, if the regex becomes
> unwieldly and I assign it to a variable:
>
> my $regex = qr{pattern};
> my $string = 'some string';
> my @match = '';
> print "@match = $string =~ $regex\n"
>
> I get a '1' returned instead of the matched bits.
No, that would interpolate @match, $string, and $regex into the
output string. It wouldn't do a regex match, or assign anything to
@match.
> If someone can let me know how to rewrite the above, I'd be grateful.
You need matching parentheses in the regex. You can interpolate a
qr// in a literal regex like a string, so this would work:
my @match = $string =~ /($regex)/
You could conceivably put the parentheses into the qr// itself, but
if this is only for debugging you don't want them there.
Quite generally it is a good idea to keep captures out of qr// expressions
(and strings that are to be interpolated in regexes) as far as possible.
To identify captures you have to count parentheses in the top-level regex.
That gets hard when parentheses are interpolated.
Anno
Post Follow-up to this messageIn <cj3qja$qhi$1@mamenchi.zrz.TU-Berlin.DE>, anno4000@lublin.zrz.tu- berlin.de wrote > Dave <dave@home.net> wrote in comp.lang.perl.misc: First, my thanks to Anno, Eric and Gunnar for the replies. It seems I made the near-fatal mistake of typing off the top of my head without considering the extacting nature of criticism to which I'd be subjected for any mistakes I'd make. Seriously, I'm sure everyone who posts questions here appreciates the effort made to educate us dolts. ;-) Hopefully this abbreviated excerpt will meet with greater approval. #!/usr/bin/perl # remove extraneous, incorrectly-formatted and banner-style # signatures from mailing list emails use strict; use warnings; my $string = "-- Unsubscribe info: http://somewebsite.com/ml/#unsubscribe-simple Problem reports: http://somewebsite.com/problems.html Documentation: http://somewebsite.com/docs.html FAQ: http://somewebsite.com/faq/ "; my $pattern = "^[> \t]*--\\s?\n[> \t]*Unsubscribe info.*somewebsite\.com\/faq\/\$"; if ( my ( @match ) = $string =~ /$pattern/smg ) { print "Unsubscribe footer matches! The match is:\n"; print "-----------------------------------------\n"; print "@match\n\n"; } else { print "No match for 'unsubscribe footer' found.\n\n"; }
Post Follow-up to this messageDave wrote: > In <cj3qja$qhi$1@mamenchi.zrz.TU-Berlin.DE>, anno4000@lublin.zrz.tu- > berlin.de wrote > > > First, my thanks to Anno, Eric and Gunnar for the replies. It seems I > made the near-fatal mistake of typing off the top of my head without > considering the extacting nature of criticism to which I'd be subjected > for any mistakes I'd make. Seriously, I'm sure everyone who posts > questions here appreciates the effort made to educate us dolts. ;-) > > Hopefully this abbreviated excerpt will meet with greater approval. > > #!/usr/bin/perl > # remove extraneous, incorrectly-formatted and banner-style > # signatures from mailing list emails > use strict; > use warnings; > > my $string = > "-- > Unsubscribe info: http://somewebsite.com/ml/#unsubscribe-simple > Problem reports: http://somewebsite.com/problems.html > Documentation: http://somewebsite.com/docs.html > FAQ: http://somewebsite.com/faq/ > "; > > my $pattern = > "^[> \t]*--\\s?\n[> \t]*Unsubscribe info.*somewebsite\.com\/faq\/\$"; You are using a double quoted string which perl will interpolate. You shoul d print it out to see how interpolation affects the regular expression. > if ( my ( @match ) = $string =~ /$pattern/smg ) { > print "Unsubscribe footer matches! The match is:\n"; > print "-----------------------------------------\n"; > print "@match\n\n"; > } else { > print "No match for 'unsubscribe footer' found.\n\n"; > } John -- use Perl; program fulfillment
Post Follow-up to this messageJohn W. Krahn wrote: > Dave wrote: > > > > You are using a double quoted string which perl will interpolate. You > should print it out to see how interpolation affects the regular > expression. Or just use qr// (probably, or maybe '').
Post Follow-up to this messageIn <%9m5d.155867$XP3.42973@edtnps84>, someone@example.com wrote > Dave wrote: > > You are using a double quoted string which perl will interpolate. I knew that, really. [sigh] > print it out to see how interpolation affects the regular expression. It worked fine, of course, but hardly an excuse for not reading more carefully or, in my case, habitually using double quotes to force unecessary interpolation thereby inviting criticism from those who know better. ;-) Thanks again.
Post Follow-up to this messageDave wrote:
> if ( my ( @match ) = $string =~ /$pattern/smg ) { ... }
I hope you realize that that can, in general, fail to match.
if ( my ( @match ) = $string =~ /\Q$pattern\E/smg ) { ... }
-Joe
Post Follow-up to this messageJoe Smith <Joe.Smith@inwap.com> wrote:
> Dave wrote:
>
>
> I hope you realize that that can, in general, fail to match.
>
> if ( my ( @match ) = $string =~ /\Q$pattern\E/smg ) { ... }
I hope you realize that m//sm are no-op modifiers if you are
going to escape dots and anchors.
:-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.