in reply to Re: Match variables : Beginner in perl -- webperl
in thread Match variables : Beginner in perl

Thanks @Discipulus , I will definitely go through that book once I get a good grip on the beginner level things , and yeah that page is super awesome , much thanks to @haukex for that , All the suggestions by fellow monks like you are really really helpful for beginners and perl enthusiasts like me. Thank you !

  • Comment on Re^2: Match variables : Beginner in perl -- webperl

Replies are listed 'Best First'.
Re^3: Match variables : Beginner in perl -- webperl
by jbodoni (Monk) on Jan 03, 2019 at 11:26 UTC

    Regex greediness: If you want to find the string one two three in "one two three" "four five six", your first thought might be to use m/"(.*)"/

    This will return one two three" "four five six because the regex will match as many characters as it can. It will match until the last " it finds.

    Instead, use m/"(.*?)"/, which will stop matching as soon as it can.

      Instead, use m/"(.*?)"/, which will stop matching as soon as it can.
      Or, better (at least when you're going up to a single-character terminator), be more explicit about what you actually want by using a negated character class: m/"([^"]*)"/

      If you want to match anything except a double-quote, tell Perl to match "anything except a double-quote" ([^"]), not "the shortest possible set of anything at all that happens to have a double-quote after it" (.*?").

      Also, more pragmatically, if there's more to your regex after this part, then you could get cases where (.*?)" will still contain double-quotes in the match if that's needed to make the "more after this part" work. Because it's more explicit about not matching double-quotes, ([^"]*) will never have them in the match.