Jump to content

Matching an Exact Length Line/String


mrhenniger

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/195423-matching-an-exact-length-linestring/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.