Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

storing string matched by regular expression

by Mahoota (Novice)
on Jun 27, 2002 at 14:29 UTC ( [id://177715]=perlquestion: print w/replies, xml ) Need Help??

Mahoota has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a regular expression that will store the actual string matched as a variable so it can be compared to another string matched in another regular expression.. I have used the following but when i print out $thisresult it seems to have stored 1 (positive) as opposed to the actual string matched. I am trying to write a script that will only print "expression found" if the string matched is not the same as the string matched previously.
$thisresult = ($teststring =~ /[aft][edt].[ftg]...v{2,3}/g); if ($thisresult) {if (!$lastresult) {print "Expression found\n"; } } $lastresult = $thisresult;
Any ideas?

edited: Thu Jun 27 14:24:25 2002 by jeffa - added code tags

Replies are listed 'Best First'.
Re: storing string matched by regular expression
by vladb (Vicar) on Jun 27, 2002 at 14:33 UTC
    Yes, I've got an idea ;).
    ($thisresult) = ($teststring =~ /[aft][edt].[ftg]...v{2,3}/g); if ($thisresult) { if (!$lastresult) { print "Expression found\n"; } } $lastresult = $thisresult;
    Notice, I simply placed your $thisresult variable inside a pair of (). This is required as the regexpt would return array of matched values in list context. Previously, you were invoking regexp in 'scalar' context, which only gets you the total count of matches found.

    Update: fixed code (saw the correct version after the original node was edited).

    _____________________
    # Under Construction
Re: storing string matched by regular expression
by jsegal (Friar) on Jun 27, 2002 at 14:36 UTC
    You need to have the expression evaluated in "list context" to get it to return what you are interested in, and you need capturing parentheses inside the pattern to show what you want to collect (in this case, I think it is your entire pattern).
    i.e.
    ($thisresult) = ($teststring =~ /([aft][edt].[ftg]...v{2,3})/g); if ($thisresult) { if (!$lastresult) { print "Expression found\n"; } } $lastresult = $thisresult;
    (Note the parentheses around $thisresult and in the pattern). If the match fails, $thisresult would be set to undef.
    See "perldoc perlop" the (section on "m") and "perldoc perlre" for more details

    --JAS
Re: storing string matched by regular expression
by mikeirw (Pilgrim) on Jun 27, 2002 at 14:37 UTC
    See perldoc perlre. Specifically, read about $1 and \1.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://177715]
Approved by little
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found