[PHP] Help with preg_match and windows domain logins...

2005-12-12 Thread Daevid Vincent
I'm trying to do what should be a very simple regex, but can't seem to get
PHP to work, yet regex-coach and even an XML .XSD work fine:

Valid forms of a windows logon are:
foo\bar
\\foo\bar


function isValidWinLogon($logon)
{
//$logon = 'foo\\bar';
$logon = '\\foo\bar'; 
print PPRElogon = '$logon'BR/PRE\n;
preg_match('/()?.+\\.+/', $logon, $matches);
//preg_match('/()?.+(\\).+/', $logon, $matches);
print_r($matches);

return (count($matches)  0);
}


I'm obviously passing in the strings, but for testing, I tried to hard-code
it. 

First, when I use single ticks, I thought that meant to be taken literally.
Seems I still have to escape (\) the backslashes.

In either case, the $matches array is EMPTY.

Further more, WTF do I get this error:
Compilation failed: missing ) at offset 12 (pointing to the commented out
preg_match.
WTF should adding a set of parenthesis cause a compilation error?!

# php -v
PHP 5.0.3 (built: Dec  1 2005 02:20:49)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies
with Zend Extension Manager v1.0.9, Copyright (c) 2003-2005, by Zend
Technologies
with Zend Optimizer v2.6.0, Copyright (c) 1998-2005, by Zend
Technologies

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



Re: [PHP] Help with preg_match and windows domain logins...

2005-12-12 Thread Robin Vickery
On 12/13/05, Daevid Vincent [EMAIL PROTECTED] wrote:
 I'm trying to do what should be a very simple regex, but can't seem to get
 PHP to work, yet regex-coach and even an XML .XSD work fine:

 Valid forms of a windows logon are:
 foo\bar
 \\foo\bar
 [...]
 //preg_match('/()?.+(\\).+/', $logon, $matches);
 [...]
 Further more, WTF do I get this error:
 Compilation failed: missing ) at offset 12 (pointing to the commented out
 preg_match.
 WTF should adding a set of parenthesis cause a compilation error?!

WTF do you need so many WTFs?

Adding a set of parenthesis isn't causing the error. Adding an
open-bracket and then an escaped close-bracket is causing your error.
The missing ) at offset 12 is a bit of a clue there.

Backslashes are special characters to both single-quoted strings AND
regular expressions so if you use them, you're normally going to have
to escape them twice.

So if you want optional double backslashes, followed by any characters
followed by a backslash followed by any characters - which is what I'm
guessing you were aiming at, you'd start off with this:

/^(\\)?.+\.+$/

you need to escape the backslashes for the regular expression engine
so you get this:

/^()?.+\\.+$/

and you're supplying it in a single-quoted string, which also needs
the backslashes escaped, so you get this:

'/^()?.+.+$/'

by which point you should be swearing at Microsoft for choosing such a
stupid character for a login string delimiter.

Clear?

 -robin