trensett wrote:

---------- Forwarded message ----------
Date: Wed, 16 Jul 2003 18:23:35 -0500 (EST)
From: trensett <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: Re: Regular Expression



On Wed, 16 Jul 2003, Janek Schleicher wrote:



Nick Diel wrote at Tue, 15 Jul 2003 11:12:18 -0600:



I am having a hard time getting a regular expression to work the way i want
it to.  Take the following text: bla bla bla (la la) bla bla (di da)

I want to extract the la la to one variable and di da to antoher variable.
What I have tried so far is using match variables, but so far I am only able
to match either just la la or la la) bla bla (di da.


It would be better if you show us not only a description of your code, but
your real code.

The next will work:
my @variable = $string =~ /\((.*?)\)/g;



what's the exact implication of ".*?"? Why wouldn't just .* in parenthesis work for this case?


The regex engine is greedy by nature, it will get the longest possible match for the given regex. In this case that will be 'la la) bla bla (di da'. The regex says get the match between a '(' and a ')' . The '? after the pattern makes it to fetch the smallest possible match i.e. disable the greediness. Try the example out with and without the '?' and check the output.


my $str = 'bla bla bla (la la) bla bla (di da)';
if ($str =~ /\((.*?)\)/) {
   print $1, "\n";
}
if ($str =~ /\((.*)\)/) {
   print $1, "\n";
}





Now $variable[0] contains 'la la' and
   $variable[1]          ' di da'.


Greetings, Janek



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]













-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to