in reply to Regex question

(?:[1-31]) #day, 1 to 31

That's not doing what you think it is. What you have there is functionally equivalent to [123] (or [1-3]).

A simple replacement would be \d+ (or, perhaps, \d\d?). But that also matches many invalid dates.

Update: Fletch fills in details that I was too lazy to :-)

--

See the Copyright notice on my home node.

"The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Regex question
by Fletch (Bishop) on Mar 22, 2007 at 14:47 UTC

    (?:[1-9]|1[0-9]|2[0-9]|3[01]) is a less simple replacement that would accept fewer invalid dates (e.g. you'd still match against 31st day of February; then again you'd want to do that validation further up the pipe anyhow).

      Another, lazier, way is to use an array and localize the default list separator

      @days = ( 1 .. 31 ); local $" = q{|}; m{(?:@days)};

      You'd probably want to limit the local $" inside a code block to avoid unexpected side effects elsewhere.

      Cheers,

      JohnGG