niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
Extract alternate patterns
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
harsha.ramanagoudra@gmail
*nix forums beginner


Joined: 17 Jul 2006
Posts: 2

PostPosted: Mon Jul 17, 2006 1:31 pm    Post subject: Extract alternate patterns Reply with 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

Thanks
Harsha
Back to top
Gunnar Hjalmarsson
*nix forums Guru


Joined: 26 Feb 2005
Posts: 852

PostPosted: Mon Jul 17, 2006 2:10 pm    Post subject: Re: Extract alternate patterns Reply with quote

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

PostPosted: Mon Jul 17, 2006 2:29 pm    Post subject: Re: Extract alternate patterns Reply with quote

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

PostPosted: Tue Jul 18, 2006 3:42 am    Post subject: Re: Extract alternate patterns Reply with quote

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 =~ /(?Sad$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

PostPosted: Tue Jul 18, 2006 4:50 am    Post subject: Re: Extract alternate patterns Reply with quote

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

PostPosted: Tue Jul 18, 2006 8:32 am    Post subject: Re: Extract alternate patterns Reply with quote

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

PostPosted: Tue Jul 18, 2006 6:28 pm    Post subject: Re: Extract alternate patterns Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 7:32 am | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts FAQ 4.34 How do I extract selected columns from a string? PerlFAQ Server Perl 0 Fri Jul 21, 2006 7:03 am
No new posts how to extract multi-line text perltcl@yahoo.com Perl 2 Wed Jul 19, 2006 4:57 am
No new posts Using tar to extract files from tape Haines Brown Debian 4 Thu Jul 13, 2006 2:20 pm
No new posts extract text between multiple char delimiters rather than... Dundonald shell 3 Tue Jul 11, 2006 1:57 pm
No new posts spam getting through in sa-exim w/ scores of 0 even with ... Eric Agnew Debian 0 Tue Jul 11, 2006 1:50 pm

Car salvage | Loans | Libros Electronicos | Cheap Magazines | Credit Cards
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3685s ][ Queries: 16 (0.2578s) ][ GZIP on - Debug on ]