Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > conditional regular expressions

Reply
Thread Tools

conditional regular expressions

 
 
Morfys
Guest
Posts: n/a
 
      08-13-2007
hi,

I was having trouble understanding how regular conditional expressions
work. In particular, in this piece of code:

$str = "ttt, 2";

if(($result) = $str =~ /\w\w\w(?(?=, )(\d))/){

print "$result\n";

}

I don't understand why there is empty output versus "2".

The desired effect is that $result contain the digit following ", " if
it exists in the string.

I've tried almost every variation of the regular expression but I'm
missing something. Thanks in advance for any help.

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      08-13-2007
Morfys wrote:
> $str = "ttt, 2";
> if(($result) = $str =~ /\w\w\w(?(?=, )(\d))/){
> print "$result\n";
> }
> I don't understand why there is empty output versus "2".
> The desired effect is that $result contain the digit following ", " if
> it exists in the string.


The regular expression is stepped through from
left to right. Therefore there is an implicit
"the more right pattern matches only if the more
left pattern already matched", which leads to the
trivial solution 1.:

my $str = 'ttt, 2';
my $result;

# trivial solution w/capturing
($result) = $str=~/\w{3}, (\d)/ and print "1. $result\n";

If you want to do some more fancy stuff, you
could 'ifify' the whole expression with
a "positive lookbehind", like:

# lookbehind (?<= ...), no capturing, /g in list context
($result) = $str=~/(?<=\w{3}, )\d/g and print "2. $result\n";

Regards

M.
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-13-2007
Morfys <(E-Mail Removed)> wrote:
> hi,
>
> I was having trouble understanding how regular conditional expressions
> work. In particular, in this piece of code:
>
> $str = "ttt, 2";
>
> if(($result) = $str =~ /\w\w\w(?(?=, )(\d))/){
>
> print "$result\n";
>
> }
>
> I don't understand why there is empty output versus "2".


?= is zero-width, meaning that after the ', ' matches, the \d is left
to match starting at the same place, namely, at the ','. Since ',' is not
a \d, it can't match.

>
> The desired effect is that $result contain the digit following ", " if
> it exists in the string.


Isn't that just what the simple /\w\w\w, (\d)/ would do?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Morfys
Guest
Posts: n/a
 
      08-14-2007
Thanks a lot! Indeed my problem was that ?= is zero-width.

> > The desired effect is that $result contain the digit following ", " if
> > it exists in the string.

>
> Isn't that just what the simple /\w\w\w, (\d)/ would do?


In fact, the example I gave was just to simplify things so that my
question could be clear. In my "real" regular expression, I need to
parse strings like
"open(X,X)"
and
"open(X,X,X)"
and so the conditional seems necessary.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-14-2007
Morfys <(E-Mail Removed)> wrote:

>> > The desired effect is that $result contain the digit following ", " if
>> > it exists in the string.

>>
>> Isn't that just what the simple /\w\w\w, (\d)/ would do?

>
> In fact, the example I gave was just to simplify things so that my
> question could be clear. In my "real" regular expression, I need to
> parse strings like
> "open(X,X)"
> and
> "open(X,X,X)"



$_ = 'open(X,Y,Z)';
my @parts = /(\w+)(?=[,)])/g;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-14-2007
Morfys wrote:
> In fact, the example I gave was just to simplify things so that my
> question could be clear.


Then you didn't succeed very well.

> In my "real" regular expression, I need to parse strings like
> "open(X,X)"
> and
> "open(X,X,X)"
> and so the conditional seems necessary.


my $str = 'open(4,5,6)';
if ( my ($digits) = $str =~ /open\((\d(?:,\d)*)\)/ ) {
my @result = split /,/, $digits;
print "@result\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      08-14-2007
Morfys wrote:
> In fact, the example I gave was just to simplify things so that my
> question could be clear. In my "real" regular expression, I need to
> parse strings like
> "open(X,X)"
> and
> "open(X,X,X)"
> and so the conditional seems necessary.


.... condition to branch on what?

I could understand this intend
to match a string like

"open(X,X,X), 3"
or
"open(X,X), 2"

but your example doesn't provide
an ample motivation for introducting
conditionals.

Regards

M.
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      08-14-2007
Tad McClellan wrote:
> Morfys <(E-Mail Removed)> wrote:
>> "open(X,X)"
>> and
>> "open(X,X,X)"

> $_ = 'open(X,Y,Z)';
> my @parts = /(\w+)(?=[,)])/g;


Just out of couriosity, what's the
purpose of these fancy parenthesis
around \w+ ? Is there any hidden
mechanics to use them this way?

Regards

M.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-14-2007
Tad McClellan wrote:
> Morfys <(E-Mail Removed)> wrote:
>> I need to parse strings like
>> "open(X,X)"
>> and
>> "open(X,X,X)"

>
> $_ = 'open(X,Y,Z)';
> my @parts = /(\w+)(?=[,)])/g;


You don't need both capturing parentheses and the "(?= ... )" part, do you?

my @parts = /(\w+)[,)]/g;

or

my @parts = /\w+(?=[,)])/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-14-2007
Mirco Wahab <(E-Mail Removed)-halle.de> wrote in comp.lang.perl.misc:
> Tad McClellan wrote:
> > Morfys <(E-Mail Removed)> wrote:
> >> "open(X,X)"
> >> and
> >> "open(X,X,X)"

> > $_ = 'open(X,Y,Z)';
> > my @parts = /(\w+)(?=[,)])/g;

>
> Just out of couriosity, what's the
> purpose of these fancy parenthesis
> around \w+ ? Is there any hidden
> mechanics to use them this way?


Hmm? What's fancy about them. They're normal capturing parentheses.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Regular Expressions in ORO Ion Freeman Java 3 07-24-2009 12:01 AM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Alternative suggestion for conditional expressions (see PEP 308) neblackcat Python 8 07-20-2004 03:24 PM
Position of test values in conditional expressions =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= C++ 23 06-28-2004 08:02 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments