From: Peter Fleck <[EMAIL PROTECTED]>
> The regex that follows is working the way I want it too (at least in
> my tests). I'm looking for feedback on better ways to write it. In
> particular, is there a shortcut to repeating my 'basic component'
> listed below? And is there a way to make the regex more readable with
> line breaks that don't become part of the expression?
>
> I'm trying to check some date input to a Web form. The formats I
> accept are 'mm/dd/yy, m/d/yy, mm/dd/yyyy, m/d/yyyy'.
>
> Here's the regex I'm using to check:
>
> (
> $date=~/^((\d\d?)\/(\d\d?)\/(((\d{2,2}))|(\d{4,4})))($|(,(\s*)((\d\d?)
> \/(\d\d?)\/(((\d{2,2}))|(\d{4,4}))))+$)/ )
>
> Basic component is:
>
> ((\d\d?)\/(\d\d?)\/(((\d{2,2}))|(\d{4,4})))
>
> then either end the line:
>
> $
>
> or go on with a comma, optional white space and repeat of the basic
> component and finally anchor to the final line end.
You may want to read up about qr// in
perldoc perlop
#somewhere on top of the script
my $date_re = '((\d\d?)\/(\d\d?)\/(((\d{2,2}))|(\d{4,4})))';
my $re = qr/^$date_re($|(,(\s*)$date_re)+$)/;
# hope I have the regexp right
...
if ($date =~ $re) {
...
Jenda
P.S.: The $date_re is just a temporary variable, so you might write
the code like this:
my $re = do {
my $date_re = '((\d\d?)\/(\d\d?)\/(((\d{2,2}))|(\d{4,4})))';
qr/^$date_re($|(,(\s*)$date_re)+$)/;
};
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]