[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • 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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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