tags:

views:

210

answers:

5

I would like to add [ and ] in my validation Regex that already accept all numeric, underscore, period and alphanumeric character.

Here is the regex working: $regex = "^[._a-zA-Z0-9-]*$";

But when I try :

$regex = "^[.\\[\\]_a-zA-Z0-9-]*$";

or

$regex = "^[.\[\]_a-zA-Z0-9-]*$";

It doesn't work (I have read to double escape in PHP for bracket but it doesn't work!

I use eregi.

Any idea?

Test :

<?php
//$regex = "^[_a-zA-Z0-9-]*$";
$regex = "^[.\\[\\]_a-zA-Z0-9-]*$";
echo("1".(eregi($regex, "patrick")?"True":"False"));//Should return True
echo("<br>");
echo("2".(eregi($regex, "p@trick")?"True":"False"));
echo("<br>");
echo("3".(eregi($regex, "pat rick")?"True":"False"));
echo("<br>");
echo("4".(eregi($regex, "patr'ick")?"True":"False"));
echo("<br>");
echo("5".(eregi($regex, "pAtr'ick")?"True":"False"));
echo("<br>");
echo("6".(eregi($regex, "pAtr_ick")?"True":"False"));//Should return True
echo("<br>");
echo("7".(eregi($regex, "pA-tr_ick")?"True":"False"));//Should return True
echo("<br>");
echo("8".(eregi($regex, "pAaAta   atrack")?"True":"False"));
echo("<br>");
echo("9".(eregi($regex, "pA%k")?"True":"False"));
echo("<br>");
echo("10".(eregi($regex, "patrick.second")?"True":"False")); //Should return True
echo("<br>");
echo("11".(eregi($regex, "[Pat]Rick")?"True":"False"));//Should return True
echo("<br>");
?>
+1  A: 

You need to escape $ if it's within double quotes.

Eg.:

$regex = "^[.\\[\\]_a-zA-Z0-9-]*\$";
troelskn
Doesn't work with the code posted in the question (I just edited it with a snippet)
Daok
You're right - Seems that PHP is smart enough to understand the missing backslash. Try using `preg_match` instead. `ereg` is deprecated.
troelskn
+1  A: 

The former one should work. You need to use two backslashes: one for the regular expression escape and one for the string escape. Just see how it gets evaluated:

echo "^[.\\[\\]_a-zA-Z0-9-]*$"; // => ^[.\[\]_a-zA-Z0-9-]*$

And that’s exactly what you need.


Edit    Use preg_match instead:

preg_match("/^[.\\[\\]_a-zA-Z0-9-]*$/i", $str)

I don’t know why this regular expression doesn’t work with eregi, but it does with preg_match. Futhermore the POSIX ERE functions are deprecated and will be removed by PHP 6 in favor of the PCRE functions. Note that PCRE regular expressions use delimiters to enclose the regular expression and separate it from the modifiers.

Gumbo
Doesn't work with the code posted in the question (I just edited it with a snippet)
Daok
It works with preg_match with : $regex = "/^[\[\]._a-zA-Z0-9-]*$/";
Daok
A: 

Not quite an answer, but a helpful tip: Use single quotes around your regexes (if they don't require php parsing), so you don't have to do an extra layer of escaping. Only a single quote requires escaping in a single-quoted string. (php strings)

Also, the ereg family of functions is on its way out. Why not use preg_match, with an 'i' after the closing slash to indicate a case-insensitive match?

grossvogel
A: 

You can't escape characters in a bracket expression when using ereg functions. Use this pattern instead:

$regex = "^[]._[a-zA-Z0-9-]*$";

You can understand why it works by reading the part on bracket expressions in regex man page.

Anyway, it would be better to just switch to PCRE functions instead, as eregi is now deprecated in PHP 5.3.

Clément
A: 

Escaping inside brackets does not work with ereg (it treats \ as literal char). ']' should always be placed as first char in brackets and '[' can be anywhere else:

$regex = "^[]._[a-zA-Z0-9-]*$";

However you better migrate to preg_match, because ereg is deprecated in PHP 5.3 and will be removed in PHP6.

Qwerty
Oops! Clément was faster :-)
Qwerty