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

New to perl and regex

by MarvC (Initiate)
on Nov 19, 2012 at 00:43 UTC ( [id://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 (Archbishop) 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:

      /[A-Za-z\s]+\s-?\d\d?/

      -- 
      Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found