Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Assigning REGEX Match to a separate variable?

by mmartin (Monk)
on Apr 02, 2012 at 16:32 UTC ( #963057=perlquestion: print w/replies, xml ) Need Help??
mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have what is probably a simple question for you all.
How can I set a separate variable to the output of a REGEX match...?

The line I'm looking at is part of an XML file, and looks something like this(below):
<String name="changeTypeName">Modified</String>"

I've tried the following but I'm getting an integer as the new variable's data instead of the string:
FYI: the array "@reportData" holds all the file's lines...
### The final result of this should be the string "Modified" from the +example line given above... for (my $x = 0; $x <= $#reportData; $x++) { if ($reportData[$x] =~ /^(\s+)(\<String name="changeTypeName"\>)/) { (my $changeType) = $reportData[$x] =~ /\>[A-z]+\</; # my $changeType = $reportData[$x] =~ /\>[A-z]+\</; # $reportData[$x] =~ /\>[A-z]+\</; # my $changeType = $reportData[$x]; # my $changeType = $1 if $reportData[$x] =~ /\>[A-z]+\</; print "Change Type is = $changeType\n"; } }
While looping through the array it finds the correct line, so the "if statement" is working correctly. But I can't seem to get the value of the REGEX match within the "if statement" to be assigned to the variable $changeType...?

If anyone has any suggestion, please let me know.

Thanks in Advance,

Replies are listed 'Best First'.
Re: Assigning REGEX Match to a separate variable?
by choroba (Bishop) on Apr 02, 2012 at 16:47 UTC
    From perlop:
    ... searches a string for a pattern match, and in scalar context returns true if it succeeds...
    Matching in list context
    If the "/g" option is not used, "m//" in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3...). (Note that here $1 etc. are also set, and that this differs from Perl 4's behavior.) When there are no parentheses in the pattern, the return value is the list "(1)" for success.
    The "/g" modifier ... In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression. If there are no parentheses, it returns a list of all the matched strings, as if there were parentheses around the whole pattern.
    So, just use
    my ($changeType) = $reportData[$x] =~ /\>([A-z]+)\</;
      Hey choroba,

      Thanks for your quick reply!!
      "perlop" that's the perldoc I was looking for... Searched every where for that. Thanks.

      Ok, cool so using the parenthesis allows you to pull out the actual matched string (i.e. $1) instead of getting just a true or false...
      Good deal... It worked! Thanks!

      Thanks Again,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963057]
Approved by moritz
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-03-19 22:56 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.