Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Match variables : Beginner in perl

by Perl_Programmer1992 (Sexton)
on Jan 03, 2019 at 06:28 UTC ( [id://1227948]=perlquestion: print w/replies, xml ) Need Help??

Perl_Programmer1992 has asked for the wisdom of the Perl Monks concerning the following question:

$dino = "I fear that I'll be extinct after a few million years."; if ($dino =~ /that (\w*) (\w*) (\w*) after/) { print "That said '$1' '$2' '$3' years.\n"; }

My question is that the above code should print I'll , be , extinct according to definition of match variables , but it's not printing anything , I want to know how perl is treating I'll , it's not a word then how we can match I'll , Kindly correct me what I am doing wrong.

Replies are listed 'Best First'.
Re: Match variables : Beginner in perl
by choroba (Cardinal) on Jan 03, 2019 at 06:44 UTC
    \w matches a "word character", but for a rather non-intuitive meaning of "word": it means it's an underscore, a digit, or a letter. Nothing more. If you want to include a single quote, you can use e.g. a character class
    [\w']

    Note that it includes quoting single quote, too. E.g.

    "I don't like 'big mouths'", said he.

    I'm not sure you'd like to treat 'big as a "word".

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Perl_Programmer1992:

      "word": it means it's an underscore, a digit, or a letter.

      Note that if you have Unicode strings, \w also includes Unicode's notion of "Word" characters - see Word characters. These are all considered "Word" characters: ξ ᚏ 𫝼 ஹ ᕭ 🅿

      By the way, you don't need to include "Beginner in perl" in your node titles :-) (see also How do I compose an effective node title?)

        Thanks @haukex , I will definitely go through the list.

        you don't need to include "Beginner in perl" in your node titles

        Sure and thanks ! :-)

      Thanks @choroba ! that was really helpful

Re: Match variables : Beginner in perl -- webperl
by Discipulus (Canon) on Jan 03, 2019 at 08:32 UTC
    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.

      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.

Re: Match variables : Beginner in perl
by hippo (Bishop) on Jan 03, 2019 at 12:31 UTC

    Sometimes it is useful to try matching the components of your dataset against a regex or two to see what works and why. eg:

    use strict; use warnings; use Test::More; for my $word (split (/ /, "I fear that I'll be extinct after a few mil +lion years.")) { like $word, qr/^\w*$/, "Match $word with \\w"; like $word, qr/^\S*$/, "Match $word with \\S"; } done_testing ();

    Without the additional anchors here (the ^ and the $) everything would match so they are just necessary to force the proper test. If you run this you will see the differences between using \w and \S to match word or non-space characters. HTH.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1227948]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 00:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found