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

Reply via email to