Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

New to perl and regex

by MarvC (Initiate)
on Nov 19, 2012 at 00:43 UTC ( #1004466=perlquestion: print w/replies, xml ) Need Help??
MarvC has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm new to Perl and regex and i cant seem to get this expression to work, I'm trying to match any amount of white space 0 or 1 times followed by any amount of words saved in a buffer(there can be any amount of space in between the words), after the words there has to be at least 1 or more white space and followed by a digit that ranges from -99 to 99 here's what I have wrote down.

print "Enter a string: "; while(<>) { $line = $_; last if ($_ =~ /^\s*[Q|q]{1,1}[U|u]{1,1}[I|i]{1,1}[T|t{1,1}\s*$/); next if ($_ =~ /^$/ or $_ =~ /^\s+/$); if($line =~ /\s*([a-zA-Z]+\s*)+\s*(-?\d\d?)\s*/) { print $_; } }

this if($line =~ /\s*([a-zA-Z]+\s*)+\s*(-?\d\d?)\s*/) is where the problem comes in. Is there any notable problems here that I'm over looking?

Replies are listed 'Best First'.
Re: New to perl and regex
by NetWallah (Canon) on Nov 19, 2012 at 02:17 UTC
    Welcome to the Monastery !

    You have other problems with unmatched "[" and the strange "$" as a regex modifier.

    I have fixed that - and modified your target selelection slightly by adding a trailing "$".

    use strict; use warnings; print "Enter a string: "; while(<>) { my $line = $_; last if ($_ =~ /^\s*[Q|q]{1,1}[U|u]{1,1}[I|i]{1,1}[T|t]{1,1}\s*$/); #Easier to match /quit/i next if ($_ =~ /^$/ or $_ =~ /^\s+$/); if($line =~ /\s*([a-zA-Z]+\s*)+\s*(-?\d\d?)\s*$/) { print $_; } }
    Now - if you supply a string of "this that other 555" it will NOT match, because it matches 2 digits, but fails on the trailing "$" in the regex.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Re: New to perl and regex
by Athanasius (Chancellor) on Nov 19, 2012 at 02:15 UTC
    if($line =~ /\s*([a-zA-Z]+\s*)+\s*(-?\d\d?)\s*/) ^

    The problem description specifies “at least 1 or more white space” between the last word and the integer, but the regex has \s* (which is zero or more). This should be \s+ (one or more).

    Apart from that, it’s difficult to offer advice without more information. Specifically, how is the code failing?

    As Kenosis says, please provide some sample data (along with the desired output).

    Athanasius <°(((><contra mundum

Re: New to perl and regex
by Kenosis (Priest) on Nov 19, 2012 at 01:37 UTC

    Hi, MarvC, and welcome to PerlMonks!

    Do you have a sample string you can share that you're trying to match? Also, help me understand what ...words saved in a buffer... means.

Re: New to perl and regex
by Utilitarian (Vicar) on Nov 19, 2012 at 08:44 UTC
    Neatened up your regular expressions a bit:
    print "Enter a string (enter quit or [CTRL]-D to quit): "; while(<>) { $line = $_; last if ($_ =~ /^\s*quit\s*$/i); next if ($_ =~ /^\s~*$/); if($line =~ /\s*[a-zA-Z\s]+\s*-?\d+\s*/) { print $_; } }
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

      I think it can be made even simpler than that:


      Ronald Fischer <>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004466]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-05-21 17:03 GMT
Find Nodes?
    Voting Booth?