Michael Alipio <daem0n...@yahoo.com> asked:
> > my $string = 'boy, pig, 123, 123:412adbd, d0g, > > lajdlf134><<_ lkadsf !234,'; > > > > if( $string =~ m/,\s*([^,]*),[^,]*$/ ){ > > print "$1\n"; > > } > > How could you guys write this so simple? My regexp was twice this long. Lots of practice? ;-) > the regexp after \s* tells perl to match anything (0 or more) that is not > a comma, right? how come it did not match pig? pig is also followed by > comma, right? so pig should be captured by ([^,]*), right? I guess perl > really looks for a match starting from the end of the line. That would be cool, right? No, my RE is says, I want a comma and maybe some whitespace after it, then I'll capture all of the following stuff that's not a comma, then I want another comma and possibly some more stuff that's not a comma right before the end of the line. Perl's RE engine starts out by looking at ", pig" (capturing "pig") but the part where I said "no further comma before the end of the line" prevents it from matching, since there's a comma after 123 but before the end of the string. For the same reasons it fails to match at 123, 123:... and d0g. That's why I think that a "classic C" approach using rindex() and substr() might be faster in your case: #!/usr/bin/perl -w use strict; my $string = 'boy, pig, 123, 123:412adbd, d0g, lajdlf134><<_ lkadsf !234,'; my $rightComma = rindex( $string, ',' ) - 1; my $leftComma = rindex( $string, ',', $rightComma ); print substr( $string, $leftComma + 2, $rightComma - $leftComma ), "\n"; __END__ HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/