Re: Regular Expression (fwd)
Trensett wrote at Wed, 16 Jul 2003 18:29:51 -0500: >> The next will work: >> my @variable = $string =~ /\((.*?)\)/g; > > what's the exact implication of ".*?"? > Why wouldn't just .* in parenthesis work for this case? A good answer can also be found in perldoc perlre and perldoc -q greedy Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expression (fwd)
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]
RE: Regular Expression (fwd)
? means minimal matching not maximal, so without the question mark it would match up to the second closing paren in the following line. ( test data ) ( more test data ) which is not what you want, it would match up to the last closing paren in the data you gave it, forgetting about newlines, with the question mark it matches up to the first, which is what you want (I assume). no ? = greedy ? = on a diet Cheers, Nigel > -Original Message- > From: trensett [mailto:[EMAIL PROTECTED] > Sent: 17 July 2003 00:30 > To: [EMAIL PROTECTED] > Subject: Re: Regular Expression (fwd) > > > > > -- 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? > > > > > > 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] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expression (fwd)
-- 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? > > 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]
Re: regular expression (fwd)
Sorry!! again, failed to send to the list -- Forwarded message -- Date: Thu, 22 Aug 2002 00:29:11 +0530 (IST) From: Sudarshan Raghavan <[EMAIL PROTECTED]> To: Javeed SAR <[EMAIL PROTECTED]> Subject: Re: regular expression On Wed, 21 Aug 2002, Javeed SAR wrote: > # I want to grep for files with .dsp or .vbp extensions here, if the > number of files with extension .dsp or .vbp is > 1 i should exit saying > project exists. or else send mail. > > am i doing the right thing? > > > @projectFilesExist = <$file\{*.dsp,*.vbp}>; This can also be written as @projectFilesExist = <$file\*.{dsp,vbp}>; > if ($projectFilesExist> 1) This should be if (@projectFilesExist > 1) > { > die "\n\nThe element ($PN) is not allowed to be added to > ClearCase,Because a project already exists.Please contact Clearcase > administrator or javeed (Extn 4919).\n"; > } > else{ > $cmd= `sendmail`; > } > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]