Printing regex match

D

Dave

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.
 
E

Eric Amick

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;
 
G

Gunnar Hjalmarsson

Dave said:
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};

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)/
 
A

Anno Siegel

Dave said:
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
 
D

Dave


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";
}
 
J

John W. Krahn

Dave said:

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
 
B

Brian McCauley

John said:
Dave said:
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.

Or just use qr// (probably, or maybe '').
 
D

Dave

Dave said:

Hopefully this abbreviated excerpt will meet with greater approval.
[...]
my $pattern =
"^[> \t]*--\\s?\n[> \t]*Unsubscribe info.*somewebsite\.com\/faq\/\$";

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.
 
J

Joe Smith

Dave said:
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
 
T

Tad McClellan

Joe Smith said:
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.

:)
 
T

Tad McClellan

Tad McClellan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top