Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.