| Author |
Message |
harsha.ramanagoudra@gmail *nix forums beginner
Joined: 17 Jul 2006
Posts: 2
|
Posted: Mon Jul 17, 2006 1:31 pm Post subject:
Extract alternate patterns
|
|
|
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 |
|
| Back to top |
|
 |
Gunnar Hjalmarsson *nix forums Guru
Joined: 26 Feb 2005
Posts: 852
|
Posted: Mon Jul 17, 2006 2:10 pm Post subject:
Re: Extract alternate patterns
|
|
|
harsha.ramanagoudra@gmail.com wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Ben Bacarisse *nix forums Guru Wannabe
Joined: 19 Jun 2005
Posts: 110
|
Posted: Mon Jul 17, 2006 2:29 pm Post subject:
Re: Extract alternate patterns
|
|
|
harsha.ramanagoudra@gmail.com wrote:
| Quote: | Hi all,
I am trying to extract alternate patterns in strings. The difficulty
is because of optional patterns.
snip
if($transaction =~ /(addReq|modReq|subtractReq)+?.*?(addReq)?/){
print "$1..$2\n";
}
snip
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.
--
Ben. |
|
| Back to top |
|
 |
attn.steven.kuo@gmail.com *nix forums addict
Joined: 15 Jul 2005
Posts: 72
|
Posted: Tue Jul 18, 2006 3:42 am Post subject:
Re: Extract alternate patterns
|
|
|
harsha.ramanagoudra@gmail.com wrote:
| Quote: | 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
--
Hope this helps,
Steven |
|
| Back to top |
|
 |
xicheng@gmail.com *nix forums Guru
Joined: 10 Nov 2005
Posts: 468
|
Posted: Tue Jul 18, 2006 4:50 am Post subject:
Re: Extract alternate patterns
|
|
|
harsha.ramanagoudra@gmail.com wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Dave Weaver *nix forums addict
Joined: 22 Apr 2005
Posts: 71
|
Posted: Tue Jul 18, 2006 8:32 am Post subject:
Re: Extract alternate patterns
|
|
|
On 17 Jul 2006 14:29:45 GMT, Ben Bacarisse <spam@bsb.me.uk> wrote:
| Quote: | snip
if($transaction =~ /(addReq|modReq|subtractReq)+?.*?(addReq)?/){
print "$1..$2\n";
}
snip
modReq..addReq
The above is matched as three parts:
"modReq" by (addReq|modReq|subtractReq)+
"..addReq" by .*
"" by (addReq)
|
No, "..addReq" is not matched by the .*
The ".*?" matches "", as does the "(addReq)?"
Witness:
% perl -le '"modReq..addReq" =~
/(addReq|modReq|subtractReq)+?(.*?)(addReq)?/ and print "1:$1 2:$2 3:$3"'
1:modReq 2: 3:
Perhaps this will help the OP:
% perl -le '"modReq..addReq" =~
/(addReq|modReq|subtractReq)+?(?=.*(addReq))?/ and print "1:$1 2:$2"'
1:modReq 2:addReq |
|
| Back to top |
|
 |
harsha.ramanagoudra@gmail *nix forums beginner
Joined: 17 Jul 2006
Posts: 2
|
Posted: Tue Jul 18, 2006 6:28 pm Post subject:
Re: Extract alternate patterns
|
|
|
Gunnar Hjalmarsson wrote:
| Quote: | snip
if($transaction =~ /(addReq|modReq|subtractReq)+?.*?(addReq)?/){
print "$1..$2\n";
}
One idea:
/(addReq|modReq|subtractReq)(?:.*(?=addReq)|.*)(addReq)?/
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
Thank you Gunnar. This works. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|