tags:

views:

110

answers:

5

Hi everyone, long time listener. First time caller...

Not strictly a PHP question as it involves regular expressions but this one has got me tearing my hair out.

I have 3 regular expressions that I want to create, and only one is working correctly.

Now I am not sure whether this is due to the fact that:

  1. I don't understand preg_match and ereg and their return codes as I haven't worked in PHP for about 7 years.
  2. My regular expressions are just plain wrong.
  3. I am mentally disabled.

Either way here are the expressions and my feeble attempts in making them work.

1) Match any number starting with 2,3,4 or 5 then followed by 5 digits. (This one I think works)

code:

if (!ereg('/[2-5]\d{5}/', $_POST['packageNumber' )
{
   echo "The package number is not the correct format.";
}

2) Match any number starting with 2,3,4 or 5 then followed by 5 digits then a period then a 1 or a 2.

if (!ereg("/[2-5]\d{5}\.[1-2]/", $_POST['packageModifier' )
{
   echo "The package modifier is not the correct format.";
}

3) Match any combination of alphanumerics, spaces,periods and hypens up to 50 characters.

if (!ereg("/[0-9a-zA-Z\s\-\.]{0,50}/", $_POST['customerNumber' )
{
   echo "The customer number is not the correct format.";
}

If anyone can please tell me what I am doing wrong I'll give them my first born.

+8  A: 

You are mixing up PCRE functions and POSIX regular expression functions. You are using a Perl-Compatible regular expression with a POSIX regular expression function.

So replace ereg by preg_match and it should work:

if (!preg_match('/^[2-5]\d{5}$/', $_POST['packageNumber'])) {
    echo "The package number is not the correct format.";
}
if (!preg_match("/^[2-5]\d{5}\.[1-2]$/", $_POST['packageModifier'])) {
    echo "The package modifier is not the correct format.";
}
if (!preg_match("/^[0-9a-zA-Z\s\-.]{0,50}$/", $_POST['customerNumber'])) {
    echo "The customer number is not the correct format.";
}

Along with fixing the PHP syntax errors I added anchors for the start (^) and the end ($) of the string to be matched.

Gumbo
+1  A: 

Don't you need to anchor the regular expressions?

Otheriwse '111111111111111211111111111' will match /[2-5]\d{5}/.

Sinan Ünür
+1  A: 

I'm assuming that you just missed off the closing ] on the $_POSTS and i've added in anchors for the start and end of the lines and used preg_match.

If you don't anchor it if the match is contained anywhere in the string then the entire thing will match. For example.

"dfasfasfasfasf25555555as5f15sdsdasdsfghfsgihfughd54" would be matched if the first one was not anchored.

Number One

if (!preg_match('/^[2-5]\d{5}$/', $_POST['packageNumber'])) {
 echo "The package number is not the correct format.";
}

Number Two

if (!preg_match('/^[2-5]\d{5}\.[2-5]$/', $_POST['packageModifier'])) {
  echo "The package modifier is not the correct format.";
}

Number Three

if (!preg_match('/^[0-9a-zA-Z\s\-.]{0,50}$/m', $_POST['customerNumber'])) {
 echo "The package modifier is not the correct format.";
}
xenon
Thank you kindly, yes the syntax errors were just a result of my quick refactoring of the code in the SO input box.
LechSzoloch
As a side note, if you need to generate this stuff all the time I recommend using a program called RegexBuddy. Very good good and you can test it live and it generates the code for you for multiple languages.
xenon
+1  A: 

When using POSIX regular expressions (deprecated by PHP 5.3) you should write the tests like this:

if (ereg('^[2-5][0-9]{5}$', $_POST['packageNumber']) === false)
{
   echo "The package number is not the correct format.";
}

if (ereg('^[2-5][0-9]{5}\\.[1-2]$', $_POST['packageModifier']) === false)
{
   echo "The package modifier is not the correct format.";
}

if (ereg('^[[:alnum:][:space:].-]{0,50}$', $_POST['customerNumber']) === false)
{
   echo "The customer number is not the correct format.";
}

Note that I anchored the regular expressions -- otherwise the customerNumber will always match (with a zero-length match).

See the POSIX regex man page for more information.

Martin Geisler
A: 
preg_match('/^[2-5]\d{5}$/', $str); // 1-st
preg_match('/^[2-5]\d{5}\.[1-2]$/', $str); // 2-nd
preg_match('/^[0-9a-z\s\-\.]{0,50}$/i', $str); // 3-rd

Your mistakes:

  1. you didn't escape slashes and finally regexp "\d" means just 'd', but "\d" means '\d'.
  2. you had to anchor regesp to beginnig and the end of entire string by ^ and $ symbols.

Thats all ;)

PS: better use single quotes for string literals - they are faster and safer...

Jet