6 Replies - 1104 Views - Last Post: 15 September 2010 - 09:23 AM Rate Topic: -----

#1 denewey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-May 09

Getting a Regular Expression to do what I want

Posted 08 September 2010 - 05:14 PM

I'm working on this project and I have to check a particular field for conformance to certain standards. The regular expression I wrote sort of does it but not completely.

The standards are this: It's a field up to 17 characters. The prefix may contain 1-3 digits, the sequence number may contain 1-8 digits, the optional suffix may contain 1 - 4 characters, and the fields are separated with a hyphen.

Some examples of correct data would be:

345-239857-4
02-65893457-239
23-45638

The regular expression I wrote to validate this is:

ereg('[0-9]{1,3}-[0-9]{1,8}-{0,1}[0-9]{0,4}', $value);



It checks for,

a) any digits - at least 1 but no more than 3.
B) a hyphen.
c) any digits - at least 1 but no more than 8.
d) a hyphen - none or 1.
e) any digit - none or no more than 4.

For the most part this works, except it goes out of it's way to make the regular expression fit. For example:

34-567-23456-1 returns true - it finds '567-23456-1' which fits all the parameters I put in the expression.

Not having done a lot of regular expressions, I'm having trouble figuring out how to have it return false if the value contains more than what is called for in it.

It even returns true on the value 29-847967857 (more than 8 digits in the second field) and I can only think that is because of the optional hyphen which then allows the expression to tack the last 4 digits on at the end without the hyphen.

I tried:
ereg('([0-9]{1,3}-[0-9]{1,8}-{0,1}[0-9]{0,4}){1}', $value);


(note the {1} at the end) to try to limit it but that didn't work either. Any ideas?

Thanks
denewey

Is This A Good Question/Topic? 0
  • +

Replies To: Getting a Regular Expression to do what I want

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Getting a Regular Expression to do what I want

Posted 08 September 2010 - 05:41 PM

How about marking the start and end of the string boundaries, like so:

<?php

$vals = array('345-239857-4',
              '02-65893457-239',
              '23-45638',
              '34-567-23456-1');

foreach ($vals as $val)
{
    echo $val . ": " . (ereg('^[0-9]{1,3}-[0-9]{1,8}-{0,1}[0-9]{0,4}$', $val) ? "OK" : "Failed") . "\n";
}

?>



yielding:
 php -f reg.php 
345-239857-4: OK
02-65893457-239: OK
23-45638: OK
34-567-23456-1: Failed


Was This Post Helpful? 1
  • +
  • -

#3 denewey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-May 09

Re: Getting a Regular Expression to do what I want

Posted 08 September 2010 - 07:18 PM

Thank you for your answer. That handled the beginning and end of the value. It'll now return false if either the prefix or the suffix are too long, but it will still accept a middle value that is greater than 8 digits, unless I take the option out for the 2nd hyphen:

ereg('^[0-9]{1,3}-[0-9]{1,8}-[0-9]{0,4}$', $value); //REMOVED THE {0,1} AFTER THE 2ND HYPHEN



This works great except it requires a hyphen even if there is no suffix to the number (which is not required)

I'll continue to check other angles, meanwhile if you have an idea, let me know.

Thanks,
denewey
Was This Post Helpful? 0
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: Getting a Regular Expression to do what I want

Posted 09 September 2010 - 11:11 AM

I don't know much about the usage of ereg as I tend to use preg and without testing I would say the following

'/^[0-9]{1,3}-[0-9]{1,8}(-[0-9]{1,4}){0,1}$/'
Was This Post Helpful? 1
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Getting a Regular Expression to do what I want

Posted 09 September 2010 - 01:19 PM

View Postgrimpirate, on 09 September 2010 - 02:11 PM, said:

I don't know much about the usage of ereg as I tend to use preg


That's okay. As of 5.3, the ereg family of RegEx functions is deprecated. They are intending to remove them entirely.

I likewise use the preg functions.

This post has been edited by Valek: 15 September 2010 - 08:43 AM

Was This Post Helpful? 1
  • +
  • -

#6 denewey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-May 09

Re: Getting a Regular Expression to do what I want

Posted 15 September 2010 - 08:20 AM

Grimpirate,

Your expression hit the mark exactly. Thank you very much! And Valek, I didn't know that the ereg expression was deprecated. I will now use preg. Thanks.

denewey
Was This Post Helpful? 0
  • +
  • -

#7 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Getting a Regular Expression to do what I want

Posted 15 September 2010 - 09:23 AM

View Postdenewey, on 15 September 2010 - 08:20 AM, said:

Grimpirate,

Your expression hit the mark exactly. Thank you very much! And Valek, I didn't know that the ereg expression was deprecated. I will now use preg. Thanks.

denewey


It's slower and more resource intensive is all. (That's why it's depricated :P)

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1