For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2004 > Printing regex match









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 Printing regex match
Dave

2004-09-25, 9:00 am

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.



Eric Amick

2004-09-25, 3:56 pm

On 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
Gunnar Hjalmarsson

2004-09-25, 3:56 pm

Dave 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
Anno Siegel

2004-09-25, 3:56 pm

Dave <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
Dave

2004-09-25, 8:56 pm

In <cj3qja$qhi$1@mamenchi.zrz.TU-Berlin.DE>, anno4000@lublin.zrz.tu-
berlin.de wrote[color=darkred]
> 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";
}



John W. Krahn

2004-09-25, 8:56 pm

Dave 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 should
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
Brian McCauley

2004-09-26, 3:55 am



John 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 '').

Dave

2004-09-26, 3:55 am

In <%9m5d.155867$XP3.42973@edtnps84>, someone@example.com wrote
> Dave wrote:
[color=darkred]
>
> 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.
Joe Smith

2004-09-27, 8:58 am

Dave 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
Tad McClellan

2004-09-27, 8:58 am

Joe 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
Tad McClellan

2004-09-27, 4:01 pm

Tad McClellan <tadmc@augustmail.com> wrote:
> Joe Smith <Joe.Smith@inwap.com> wrote:
>
>
> I hope you realize that m//sm are no-op modifiers if you are
> going to escape dots and anchors.



Errr, and even more importantly, @match isn't likely to have
very interesting contents if we backslash memory parens.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com