regex problem

Posted on 2005-04-19
Last Modified: 2010-03-05
why does the regex not match the expression?
$i='09 Jan 2001';
if ($i=~(/[0|1|2|3]?\d\s+[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]\s+\d{4}/)){
print "z";
Question by:worldfear
    LVL 19

    Accepted Solution

    You need normal brackets around the month alternation, not square brackets
    if ($i=~/[0|1|2|3]?\d\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{4}/)

    this is a bit shorter
    if ($i=~/[0-3]?\d\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{4}/)
    LVL 84

    Assisted Solution

    for $i ( '|9 | 2001', '09 J 2001', '39 a 2001', '0 n 2001' ){
      if ($i=~(/[0|1|2|3]?\d\s+[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]\s+\d{4}/)){
    print "z";
    LVL 16

    Assisted Solution

    if ($i=~(/[0123]?\d\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{4}/))

    - characters inside a class ([]) lost their special meaning. Hence, [0|1|2|3] matches 1 or 2 or 3 or |
    - the pipe can be used inside () to make a 'or' condition.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now