in reply to Regexp problem

Please use [] to separate the separator choices. Please consider using \A and \Z to test the entire input string. I think you meant ? rather than * for the final optional section? '?' means optionally, while * means zero or more.

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump); my @d = qw(26-04 26-04-12 26.04 26.04.12 30/8 30/08 30.08/12 1 12 aa a +a/bb aa.c help); say (/\A\d{2}[.-]\d{2}([.-]\d{2})?\Z/ ? "Matches for $_" : "FAILS for +$_" ) for @d;

Produces:

Matches for 26-04
Matches for 26-04-12
Matches for 26.04
Matches for 26.04.12
FAILS for 30/8
FAILS for 30/08
FAILS for 30.08/12
FAILS for 1
FAILS for 12
FAILS for aa
FAILS for aa/bb
FAILS for aa.c
FAILS for help

Replies are listed 'Best First'.
Re^2: Regexp problem
by kcott (Archbishop) on Aug 28, 2012 at 16:24 UTC
    "Please consider using \A and \Z to test the entire input string."

    I agree with this in principle; however, there's a subtle difference between \Z (uppercase) and \z (lowercase).

    • /\A ... \z - matches the entire input string.
    • /\A ... \Z - matches the entire input string except for a terminal newline, if it exists.

    Here's a couple of one-liners to demonstrate this:

    $ perl -E 'my $x = qq{qwerty\n}; $re = qr{\Aqwerty\Z}; say +($x =~ /$r +e/) ? 1 : 0;' 1 $ perl -E 'my $x = qq{qwerty\n}; $re = qr{\Aqwerty\z}; say +($x =~ /$r +e/) ? 1 : 0;' 0

    See Assertions under perlre - Regular Expressions which has:

    "To match the actual end of the string and not ignore an optional trailing newline, use \z."

    -- Ken