Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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.

Comment on Regex in if statemetn
Download Code
Re: Regex in if statement
by toolic (Chancellor) on Mar 18, 2014 at 19:41 UTC
      Doesn't seem to work. #MO and # MO do not match. Tried the regex tester at http://www.internetofficer.com/seo-tool/regex-tester/ and it says "no match"

        Sure they do:

        $ cat 1078855.pl use warnings; use strict; while (<DATA>) { print if /^#\s*[a-z]+/i; } __DATA__ #MO # MO #mo # mo # 555 $ perl 1078855.pl #MO # MO #mo # mo $

        Hi finfan,

        ..Doesn't seem to work. #MO and # MO do not match. Tried the regex tester at http://www.internetofficer.com/seo-tool/regex-tester/ 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
        "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 http://www.internetofficer.com/seo-tool/regex-tester/ 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 (Curate) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-22 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls