Problem extracting values from regex variables

if ($part2 =~ (/A[BGIP]001/)|(/T[ER]001/)|(/CP002/)|(/MF00[12]/)|
(/OS001/)|(/T[ER]001/)|(/SY001/)) {

I'm using the regex above to find out if any of the listed message codes are in the current input line. What I need to know, is: If there is a match, is there any way to extract whatever matched and put it into a variable? I know that I could put in an if then else set of structures for every message type, but that's too cumbersome. I've also tried what's listed below, but it doesn't work either:


if ($part2 =~ (/A[BGIP]001/)|(/[b][color=Red]T[ER]001[/color][/b]/)|(/CP002/)|(/MF00[12]/)|
(/OS001/)|(/T[ER]001/)|(/SY001/)) {
print $part2,"
";
print "$1: ",$1,"
";

What [italic]should[/italic] print is:
part2: UBMIT_STOCK_ORDER SIS Reply: [b]TR001[/b]BO00003486664721030234007C

[b][color=Red]$1: TR001[/color][/b]

What does print is:
part2: UBMIT_STOCK_ORDER SIS Reply: TR001BO00003486664721030234007C

[b][color=Red]$1:[/color][/b]

TR001, which is one of the things the regex match is looking for is in variable $part2. If I read the regex match info correctly, the value "TR001" should be in variable $1, but in this case it is blank. Any ideas what's wrong?
Any ideas, suggestions, or comments will be greatly appreciated.
PETERV

Comments

  • : if ($part2 =~ (/A[BGIP]001/)|(/T[ER]001/)|(/CP002/)|(/MF00[12]/)|
    : (/OS001/)|(/T[ER]001/)|(/SY001/)) {
    :
    : I'm using the regex above to find out if any of the listed message
    : codes are in the current input line. What I need to know, is: If
    : there is a match, is there any way to extract whatever matched and
    : put it into a variable? I know that I could put in an if then else
    : set of structures for every message type, but that's too cumbersome.
    : I've also tried what's listed below, but it doesn't work either:
    :
    :
    : if ($part2 =~
    : (/A[BGIP]001/)|(/[b][color=Red]T[ER]001[/color][/b]/)|(/CP002/)|(/MF0
    : 0[12]/)|
    : (/OS001/)|(/T[ER]001/)|(/SY001/)) {
    : print $part2,"
    ";
    : print "$1: ",$1,"
    ";
    :
    : What [italic]should[/italic] print is:
    : part2: UBMIT_STOCK_ORDER SIS Reply:
    : [b]TR001[/b]BO00003486664721030234007C
    :
    : [b][color=Red]$1: TR001[/color][/b]
    :
    : What does print is:
    : part2: UBMIT_STOCK_ORDER SIS Reply: TR001BO00003486664721030234007C
    :
    : [b][color=Red]$1:[/color][/b]
    :
    : TR001, which is one of the things the regex match is looking for is
    : in variable $part2. If I read the regex match info correctly, the
    : value "TR001" should be in variable $1, but in this case it is
    : blank. Any ideas what's wrong?
    : Any ideas, suggestions, or comments will be greatly appreciated.
    : PETERV

    Peter,

    This works for me:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $part2 = "UBMIT_STOCK_ORDER SIS Reply: TR001BO00003486664721030234007C";

    if ($part2 =~ /(A[bgip]001)|(T[er]001)|(CP002)|(MF00[12])|
    (OS001)|(T[er]001)|(SY001)/i) {
    print $part2,"
    ";
    print "$2: ",$2,"
    ";
    }

    Notice that I've reworked the regex a little, made it case insensitive (to match the capital 'R') and you'll notice that regex's assign matches to the dollar vars from left to right. As your TR001 match is in the 2nd from the left, this makes it $2.

    Hope that helps

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion