The reason that "200412" matches in your first regex is that the first four characters match the pattern (as expected) but there is nothing in the regex that causes the additional characters "12" to result in  a mismatch; the regex engine simply ignores them. Adding "$" to the regex (as Mike has done) will disallow any additional characters in the string, and should work as you expected.

Furthermore, using * instead of {0,2} will match strings such as 2004-12-01-02-03.



<[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]

01/27/2005 11:06 AM

       
        To:        <[EMAIL PROTECTED]>, <perl-win32-users@listserv.ActiveState.com>
        cc:        
        Subject:        RE: Regex Help



Dirk,

I'm not a regex pro, but this worked for me: /^\d{4}(-\d{2}){0,2}$/

I tested it with this:

@test = qw( xxxx 2004 200412 20041201 2004-12 2004-12-01 );

foreach $test (@test) {
                print "$test : " . ($test =~ /^\d{4}(-\d{2}){0,2}$/ ? "TRUE" : "FALSE") . "\n";
}


Regards,
Mike

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of
Dirk Bremer
Sent: Thursday, January 27, 2005 10:43 AM
To: perl-win32-users
Subject: Regex Help



I need to test for the following date-type strings:

2004
2004-12
2004-12-01

All of the above would be legal, any other combination would be illegal.

I have been using the following data to test the regex:

'xxxx'        Should produce a false result.
'2004'        Should produce a true result.
'200412'      Should produce a false result.
'20041201'    Should produce a false result.
'2004-12'     Should produce a true result.
'2004-12-01'  Should produce a true result.

My first attempt at a regex is /\d{4}(-\d{2})*/. This produces the
following result:

xxxx is false
2004 is true
200412 is true
20041201 is true
2004-12 is true
2004-12-01 is true

I would prefer that '200412' and '20041201' would fail, i.e. be reported
as false. I'm not sure what's going on with this regex as I would expect
the following:

                match the first four numeric characters, then match zero or more
occurrences of a dash character followed by two numeric characters

The next regex /\d{4}(-\d{2})+/ produces the following results:

xxxx is false
2004 is false
200412 is false
20041201 is false
2004-12 is true
2004-12-01 is true

This is closer, but I also want '2004' to be true.

The next regex /\d{4}(?=-\d{2})/ produces the same results as the
previous regex:

xxxx is false
2004 is false
200412 is false
20041201 is false
2004-12 is true
2004-12-01 is true

I've done quite a few regexes in the past but have never used the
positive lookahead assertion before because I have never really
understood it.

Out of the three regexes, I would have expected the first to fulfill my
requirements. I do not understand why it is not, although I suspect it
has something to do with the dash character.

What am I doing wrong here? I should be able to accomplish this test
using a single regex, right?

Dirk Bremer - Systems Programmer II - ESS/AMS  - NISC St. Peters
USA Central Time Zone
636-922-9158 ext. 8652 fax 636-447-4471

[EMAIL PROTECTED]
www.nisc.cc

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
******************************************************************************************
The information contained in this message, including attachments, may contain
privileged or confidential information that is intended to be delivered only to the
person identified above. If you are not the intended recipient, or the person
responsible for delivering this message to the intended recipient, ALLTEL requests
that you immediately notify the sender and asks that you do not read the message or its
attachments, and that you delete them without copying or sending them to anyone else.


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to