• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

regex problem

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";
}
0
worldfear
Asked:
worldfear
3 Solutions
 
Kim RyanIT ConsultantCommented:
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}/)
0
 
ozoCommented:
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";
  }
}
0
 
manav_mathurCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now