Jump to content

Match a number of integers in a string


Mido

Recommended Posts

Hello, I'm a beginner in regex. Basically, what I want is to match a specified number of integers in a string. Like, :

$str="123-456-789" // phone number... 

I want to check, via preg_match(), whether the string contains [7-15] digits. So, 123-456-789-321-654 would be valid; they contain 20 characters, but only 15 digits, so it's within the interval range.

 

I thought about validating the string then stripping out the hyphens/spaces then count the final filtered version of the string. But I'd rather do them all in one regex pattern. Is there a way to do that?

 

If it wasn't clear enough, please tell which part wasn't. Thanks.

Link to comment
Share on other sites

Well OK, that's a like it is *technically* possible...

 

 

$pattern = '#^[^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*$#';

Link to comment
Share on other sites

  • 3 weeks later...
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.