Boris Shor wrote: > Hello, > > Perl beginner here. I am having difficulty with a regular expression > that uses non-greedy matches. Here is a sample code snippet:
> > $test = "Yea 123xrandomYea 456xdumdumNay 789xpop"; > while ($test =~ /Yea (.*?)x.*?(?:Nay (.*?)x)?/g) You have your non-capturing test in the wrong place. See below. > { > print "$1\n"; > print "$2\n"; > } The first problem is that you are not using strict or warnings: Greetings! E:\d_drive\perlStuff>perl -w $test = "Yea 123xrandomYea 456xdumdumNay 789xpop"; while ($test =~ /Yea (.*?)x.*?(?:Nay (.*?)x)?/g) { print "$1\n"; print "$2\n"; } ^Z 123 Use of uninitialized value in concatenation (.) or string at - line 5. 456 Use of uninitialized value in concatenation (.) or string at - line 5. > The idea is that every 'phrase' is delimited with an 'x' and there are > random letters between phrases. Well, there seems top be more to what you want than that. You seem to want clauses that begin with either Yay or Nay and are followed by a numerical expression. > > > I expect to see: > > 123 > > 456 > 789 I'm not sure what you are looking for here. > > > But instead I get: > > 123 > > 456 > > Why don't I get the second part of the regular expression? Does this work for you? Greetings! E:\d_drive\perlStuff>perl -w -Mstrict my $test = "Yea 123xrandomYea 456xdumdumNay 789xpop"; while ($test =~ /(?:Yea|Nay)\s*(.*?)x/g) { print "$1\n"; } ^Z 123 456 789 Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>