[PHP] What is wrong with this preg_match?

2011-10-27 Thread Paul Halliday
I have the following:

if (isset($argc)) {
if ($argc == 1 || $argc  2 || !preg_match((\d{4}-\d{2}-\d{2}),
$argv[1])) {
echo \nUsage: $argv[0] -mm-dd\n\n;
exit;
} else {
$base_date = $argv[1];
}
} else {
$base_date = date('Y-m-d');
}

When I run it:

 $ ./process_patches.php 201-01-01

Usage: ./process_patches.php -mm-dd

patches@innm2 ~/Code/Oculi $ ./process_patches.php 2011-011-01

Usage: ./process_patches.php -mm-dd

patches@innm2 ~/Code/Oculi $ ./process_patches.php 2011-01-011

Works..

What am I doing wrong?

Thanks!

-- 
Paul Halliday
http://www.squertproject.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] What is wrong with this preg_match?

2011-10-27 Thread Steve Staples
-Original Message-
From: Paul Halliday [mailto:paul.halli...@gmail.com] 
Sent: Thursday, October 27, 2011 2:43 PM
To: PHP-General
Subject: [PHP] What is wrong with this preg_match?

I have the following:

if (isset($argc)) {
if ($argc == 1 || $argc  2 || !preg_match((\d{4}-\d{2}-\d{2}),
$argv[1])) {
echo \nUsage: $argv[0] -mm-dd\n\n;
exit;
} else {
$base_date = $argv[1];
}
} else {
$base_date = date('Y-m-d');
}

When I run it:

 $ ./process_patches.php 201-01-01

Usage: ./process_patches.php -mm-dd

patches@innm2 ~/Code/Oculi $ ./process_patches.php 2011-011-01

Usage: ./process_patches.php -mm-dd

patches@innm2 ~/Code/Oculi $ ./process_patches.php 2011-01-011

Works..

What am I doing wrong?

Thanks!

--
Paul Halliday
http://www.squertproject.org/


Paul,

To me, it looks like you're just getting the next 2 digits, so 2011-01-011 is 
getting 2011-01-01 and truncating the last 1.

If you had used (I think)  ^(\d{4}-\d{2}-\d{2})$  I think that would give you 
what you want... (but my reg-ex is horrible)

Steve

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] What is wrong with this preg_match?

2011-10-27 Thread Robert Williams
On 10/27/11 11:43, Paul Halliday paul.halli...@gmail.com wrote:


if ($argc == 1 || $argc  2 || !preg_match((\d{4}-\d{2}-\d{2}),

Usage: ./process_patches.php -mm-dd

patches@innm2 ~/Code/Oculi $ ./process_patches.php 2011-01-011

The problem is that your expression basically defines a 'contains'-type
search, so it's matching the first 10 characters as required but then
simply ignoring that last, 11th character because there's no requirement
regarding it in the expression.

Try this instead:

^(\d{4}-\d{2}-\d{2})$

The ^ anchors the matching string to the beginning of the string, while
the $ anchors it to the end, which effectively forces the expression to
match the entire string, disallowing extra characters before or after it.


Regards,
Bob
--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php