Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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




Report this thread to moderator Post Follow-up to this message
Old Post
Dave
09-25-04 02:00 PM


Re: Printing regex match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Amick
09-25-04 08:56 PM


Re: Printing regex match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
09-25-04 08:56 PM


Re: Printing regex match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Anno Siegel
09-25-04 08:56 PM


Re: Printing regex match
In <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";
}




Report this thread to moderator Post Follow-up to this message
Old Post
Dave
09-26-04 01:56 AM


Re: Printing regex match
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 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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
09-26-04 01:56 AM


Re: Printing regex match

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


Report this thread to moderator Post Follow-up to this message
Old Post
Brian McCauley
09-26-04 08:55 AM


Re: Printing regex match
In <%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.

Report this thread to moderator Post Follow-up to this message
Old Post
Dave
09-26-04 08:55 AM


Re: Printing regex match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Joe Smith
09-27-04 01:58 PM


Re: Printing regex match
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

Report this thread to moderator Post Follow-up to this message
Old Post
Tad McClellan
09-27-04 01:58 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.