Author Topic: Matching an Exact Length Line/String  (Read 524 times)

0 Members and 1 Guest are viewing this topic.

Offline mrhennigerTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Matching an Exact Length Line/String
« on: March 16, 2010, 07:55:09 AM »
I am new to regular expressions but I hope to build some competance on the subject.  For now though I am just a newbie.

I am trying to create a regex were the haystack is expected to be small (only 10 characters or less), and a match is to be confirmed when the subject string fits the full length of the haystack.  Here is the regex I have created in PHP...

$number = preg_match("/^(N[CGLRSX]{0,1}[0-9]{1-3}[A-Z]{0,2})$/", $aCR);

When I provide the values "N1Z" for $aCR, I expect to get a value of 1 for $number, but instead I get 0.   Hmmmm....  What it looks straight forward, but obviously I am doing something wrong.   Can anyone point out the error of my ways?

Thanks in advance for any assistance you can provide.

Mike
« Last Edit: March 16, 2010, 07:55:51 AM by mrhenniger »

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Matching an Exact Length Line/String
« Reply #1 on: March 16, 2010, 08:30:07 AM »
Your concept is actually right, but I think you made a typo. {1-3} should obviously be {1,3}. I believe {1-3} would match itself as a literal string so your $aCR would have to be something like "N1{1-3}Z" in order to match the pattern.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline mrhennigerTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: Matching an Exact Length Line/String
« Reply #2 on: March 16, 2010, 08:17:48 PM »
That was it!  If it wasn't for all of my typos I would be a genius.  ::)

Thanks.

Mike

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Matching an Exact Length Line/String
« Reply #3 on: March 16, 2010, 08:24:23 PM »
Wouldn't we all :)
CodeCanyon - Cheap, High Quality, Ready Made Scripts.