Extract alternate patterns

  • Thread starter harsha.ramanagoudra
  • Start date
H

harsha.ramanagoudra

Hi all,
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..

My intention is that this search extracts patterns like those below. In
some cases $2 may be undef

addReq
addReq..addReq
modReq..addReq
subtractReq

Thanks
Harsha
 
G

Gunnar Hjalmarsson

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)?/
 
B

Ben Bacarisse

Hi all,
I am trying to extract alternate patterns in strings. The difficulty
is because of optional patterns.
if($transaction =~ /(addReq|modReq|subtractReq)+?.*?(addReq)?/){
print "$1..$2\n";
}
The output I get is

addReq..

My intention is that this search extracts patterns like those below. In
some cases $2 may be undef

addReq
addReq..addReq
modReq..addReq

The above is matched as three parts:

"modReq" by (addReq|modReq|subtractReq)+
"..addReq" by .*
"" by (addReq)

There are several fixes, but the simplest would be to be more
restrictive than .* in the middle part.
 
A

attn.steven.kuo

Hi all,
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..

My intention is that this search extracts patterns like those below. In
some cases $2 may be undef


It's not absolutely clear what you expect
the output to be. In any case, this
may be one of those instances where it's
easlier to use pattern matches against
the reversed string:

#!/usr/bin/perl
use strict;
use warnings;

my $alternation =
join '|' => map { scalar reverse $_ }
(qw/foo bar baz/);

my $qr1 = qr/$alternation/;

my $last = reverse "foo";
my $qr2 = qr/$last/;

while (<DATA>)
{
chomp;
my $target_string = reverse $_;
print $_, "\n";

show_match("\tA:")
if ($target_string =~ /($qr2)?(?=.*($qr1))/);

show_match("\tB:")
if ($target_string =~ /(?:($qr2)|(?!.*$qr2.))(?=.*($qr1))/)
}

sub show_match
{
my $tag = shift;
my $first = scalar reverse $2;
my $last = $1 ? scalar reverse $1 : '';
print "$tag $first and $last\n";
};

__DATA__
baz foo baz foo baz
foo bar foo
foo
foo foo
foo bar baz baz
baz foo
 
X

Xicheng Jia

Hi all,
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..

My intention is that this search extracts patterns like those below. In
some cases $2 may be undef

addReq
addReq..addReq
modReq..addReq
subtractReq

Your problem lies in those lazy quantifiers, if there is not any
constriant behind those (...)? constructs, they match nothing by
default. so a simply fix to your problem is changing the last (...)?
construct to an alternation ( .. | .. ) like:

if ($transaction =~ /(addReq|modReq|subtractReq).*?(addReq|$)/)

Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top