Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex matching question

Status
Not open for further replies.

peterv6

Programmer
Sep 10, 2005
70
US
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'd appreciate any ideas.
PETERV

PETERV
Syracuse, NY &
Boston, MA
 
Kevin,
I read the perldoc info you listed, and according to that, the following code I used should have worked, but does not:

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

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

$1: TR001

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


$1:

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?

PETERV
Syracuse, NY &
Boston, MA
 
Peter,

you may have read the tutorial, but I guess you did not understand it. The whole construct is wrong anyway. I'm surprised this is not returning an error:

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

should be:

Code:
[blue]$part2[/blue] = [red]'[/red][purple]part2: UBMIT_STOCK_ORDER SIS Reply: TR001BO00003486664721030234007C[/purple][red]'[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$part2[/blue] =~ [red]/[/red][purple](A[BGIP]001|CP002|MF00[12]|OS001|T[ER]001|SY001)[/purple][red]/[/red][red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$part2[/blue],[red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\$[/b][/purple]1:  [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

The above will return true for the first pattern found and not continue to look for more patterns.

Note that you have the same pattern twice in the regexp: T[ER]001, you only need to have it once.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,
I believe I understood the intent of the info, but I messed up the syntax of the regex itself. I appreciate you explaining it the way you did. Following your advice, what I was trying to accomplish is working perfectly. Thank you very much for taking the time to clarify the issue!

PETERV
Syracuse, NY &
Boston, MA
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top