matching date

J

joez311

Hi,
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds. Could some one show me the correct way
to pattern match on this?
Thanks,
Zim
 
D

David Squire

Hi,
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds. Could some one show me the correct way
to pattern match on this?

Hmmm. Very hard to know exactly what you want without a real Perl code
example (please read the posting guidelines for this group). Perhaps you
want something like:

my ($month, $day, $year) = ($date_string =~
m|([01][0-9])/([0-3][0-9])/([0-9][0-9])|); # Untested. Assumes there
will always be two characters per field

Note that you can use delimiters other than '/' for your regular
expression if you start with 'm' explicity.

There are also several powerful modules on CPAN for parsing dates and
times. It would be a good idea to use one of those for any serious work.


DS
 
T

Tad McClellan

I want to
match on the date went its formated like mm/dd/yy.


print "$_ looks like a date\n" if m#^\d\d/\d\d/\d\d$#; # untested

I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
^^^^^ ^^ where is the closing square bracket?
^^^^^ matches any one of *three* characters, it will match a comma...

but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds.


Either put a backslash in front of the slashes, or use an alternate
delimiter for the match operator.
 
D

Dr.Ruud

(e-mail address removed) schreef:
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds. Could some one show me the correct way
to pattern match on this?
Thanks,
Zim


I see a comma that probably shouldn't be there, and a missing "]", and
unescaped slashes, which leads to:

m~[01][0-9]/[0-3][0-9]/[0-9][0-9]~



Check out Regexp::Common
http://search.cpan.org/search?module=Regexp::Common
 
P

Paul Lalli

Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds.

You have multiple problems.
1) if you use / as the delimiter, you have to escape (that is, precede
with a backslash) any actual / characters that you want to match.
2) commas match themselves in character classes. A character class is
either a list (with nothing separating the elements) or a range (using
the - character) or a combination of both
3) Not all of your character classes are terminated
4) The character class [0-9] is more easily written \d

Putting those together:
m/[01]\d\/[0-3]\d\/\d\d/;

However, this is still not sufficient to match a real date, as it would
allow a date of, for example,
19/38/04;
Could some one show me the correct way
to pattern match on this?

The correct way is to not reinvent the wheel:

use Regexp::Common qw/time/;
/$RE{time}{mdy}/;
to allow any month-day-year pattern, or
/$RE{time}{m2d2y2}/
to specify exactly two digits for each field.

Paul Lalli
 
U

usenet

match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds.

Since you use "/" to delimit your pattern, Perl finds the next "/" and
thinks the pattern specification has ended. You can use another
character to delimit the match, but you must actually put "m" in front
of it, such as using bangs:
m!\d\d/[0-3]\d/\d\d!;

Or you can escape the embedded slashes:

/d\d\/[0-3]\d\/\d\d/;

Or you can use Regexp::Common from CPAN...

BTW, you didn't close your second-to-last character class.
 
I

it_says_BALLS_on_your forehead

Dr.Ruud said:
(e-mail address removed) schreef:
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds. Could some one show me the correct way
to pattern match on this?
Thanks,
Zim


I see a comma that probably shouldn't be there, and a missing "]", and
unescaped slashes, which leads to:

m~[01][0-9]/[0-3][0-9]/[0-9][0-9]~

one issue with the OP's regex (even after it's been "corrected") is
that it matches months 13 - 19, and days 32 - 39 (and also day 31 for
months that don't have that particular day).
 
B

Brian McCauley

Hi,
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because of
the / used to split the feilds.

Well yes the unescaped / would be a problem if you are also using it as
your pattern demiliter.

As would the missing ]

And I somehow doubut you really want to allow the first character to be
a comma.

Fix all that and your pattern will still match 19/39/06

So why not simply m{\d\d/\d\d/\d\d}?

Fundamentally though you have an insummountable problem trying to match
a date only when it's fomatted mm/dd/yy since there are strings like
"01/02/03" that look like valid dates in yy/mm/dd or dd/mm/yy format
too.

Perhaps if we knew what you really wanted to do...
 
D

Dr.Ruud

it_says_BALLS_on_your forehead schreef:
Dr.Ruud:
joez311:
Can someone help me out with the following pattern match? I want to
match on the date went its formated like mm/dd/yy. I was trying:
/[0,1][0-9]/[0-3][0-9]/[0-9[0-9]/
but my match doesn't seam to work as expected. I think its because
of the / used to split the feilds. Could some one show me the
correct way to pattern match on this?

I see a comma that probably shouldn't be there, and a missing "]",
and unescaped slashes, which leads to:

m~[01][0-9]/[0-3][0-9]/[0-9][0-9]~

one issue with the OP's regex (even after it's been "corrected") is
that it matches months 13 - 19, and days 32 - 39 (and also day 31 for
months that don't have that particular day).

That was the main reason for this advice:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top