Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Regex in if statemetn

by finfan (Acolyte)
on Mar 18, 2014 at 19:15 UTC ( #1078850=perlquestion: print w/replies, xml ) Need Help??
finfan has asked for the wisdom of the Perl Monks concerning the following question:

I'm pulling my hair out, this seems so simple. I need to check the variable $index$an. I'm looking for any of these... # MO (any two caps letters with a # and space first) #MO (same thing with no space) # mo #mo

Here's what I'm using...

if (($input[$an] =~ /^# [a-z]/) || ($input[an] =~ /^#[A-Z]/)|| ($input[an] =~ /^#[a-z]/)|| ($input[an] =~ /^# [A-Z]/)) { ... }

Problem is, even and IP address is matching as long as it has a # in front of it. Can't understand why this would match an ip.

Replies are listed 'Best First'.
Re: Regex in if statement
by toolic (Bishop) on Mar 18, 2014 at 19:41 UTC
      Doesn't seem to work. #MO and # MO do not match. Tried the regex tester at and it says "no match"

        Hi finfan,

        ..Doesn't seem to work. #MO and # MO do not match. Tried the regex tester at and it says "no match"..

        I know the code posted by toolic worked, I wanted to find out why it wasn't working on the website you mentioned. Tried it out and it worked with the following result:

        Regular Expression Test Results Tested pattern: ^#\s*[a-z]+ Applied options: the comparison was case-insensitive String 1: #MO Result : true String 2: # MO Result : true String 3: #mo Result : true String 4: # mo Result : true String 5: # 555 Result : false
        May be the question, one should be asking is: How are you doing these regexp?

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

        Sure they do:

        $ cat use warnings; use strict; while (<DATA>) { print if /^#\s*[a-z]+/i; } __DATA__ #MO # MO #mo # mo # 555 $ perl #MO # MO #mo # mo $
        "Doesn't seem to work. #MO and # MO do not match."

        That sounds like you forgot the 'i' modifier at the end of "/^#\s*[a-z]+/i".

        "Tried the regex tester at and it says "no match""

        That site clearly states "We use Perl-style regular expressions." (not Perl regular expressions). It also seems to have limited functionality: it has a checkbox for the 'i' modifier and nothing for any of the other modifiers (see /PATTERN/msixpodualgc in "perlop: Regexp Quote-Like Operators").

        Why not try it in a real perl script. As ++toolic provided you with the complete code, it's a simple copy-paste operation.

        -- Ken

Re: Regex in if statemetn
by Anonymous Monk on Mar 18, 2014 at 20:07 UTC

    You're right, it shouldn't match an IP (unless you mean hostname?). Please provide some more sample input with comments on what should match and what shouldn't.

    With the limited specs you've given, here's a regex: /^# ?(?:[A-Z]{2}|[a-z]{2})/ That'll match any two uppercase letters or any two lowercase letters, but not mixed ("Mo"). If you want mixed case to match too: /^# ?[A-Z]{2}/i If you want to match only two letters, not more, stick a \b at the end of the regex.

Re: Regex in if statemetn
by frozenwithjoy (Priest) on Mar 19, 2014 at 12:42 UTC
    Just in case you copied this snippet directly from your script, I wanted to let you know that in one place you are using $input[$an] and everywhere else using $input[an] (Note $an vs an)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1078850]
Approved by toolic
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-05-27 14:05 GMT
Find Nodes?
    Voting Booth?