Re: Extract alternate patterns



harsha.ramanagoudra@xxxxxxxxx wrote:
I am trying to extract alternate patterns in strings. The difficulty
is because of optional patterns.

Below is the code,$transaction is the search string.

if($transaction =~ /(addReq|modReq|subtractReq)+?.*?(addReq)?/){
print "$1..$2\n";
}

The above code is succeeding to match the first pattern but always
fails to match the optional second addReq. The output I get is

addReq..

One idea:

/(addReq|modReq|subtractReq)(?:.*(?=addReq)|.*)(addReq)?/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
.