in reply to Match variables : Beginner in perl

Hello Perl_Programmer1992 and accept my welcome to the monastery and to the wonderful world of Perl!

Let's start from choroba's answer (note: I prefere square brackets to print variables): see it here live at WebPerl (WebPerl kindly provided by haukex)

The above works as you expected. But be careful with the * quantifier: it's greedy, very greedy! Learn about greediness at ModernPerl book .. well read all the book as side book while learning perl: it is not designed for beginner but is a great book, and free!

Also notice that you worked on a positive match, but you can also do the opposite: work on negative matches, ie match all non spaces \S like in this example

haukex was so kind to provide also a regex testing page: let's see our last regex against your string and another similar to choroba's one: regex live at webperl

As you can see there is a lot to play with!

HtH

PS if interested you can create links to webperl using my WebPerlizator - automating WebPerl (by haukex) page creation

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Match variables : Beginner in perl -- webperl
by Perl_Programmer1992 (Sexton) on Jan 03, 2019 at 09:18 UTC

    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 !

      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.