Jump to content

Up to 6 Numbers followed by a letter


DanRz
Go to solution Solved by kicken,

Recommended Posts

Hi,

I have the following expression but it doesn't work as I wish it to...

[1-9]\d{5}M

I need any 6 numbers followed by an M.... however, it can be any 5 numbers followed by an M... for example

123456M = Valid
1234M = Valid

But...
123445 = Invalid
RANDOM = Invalid

Thanks

Link to comment
Share on other sites

  • Solution
3 hours ago, DanRz said:
\d{5}

The braces specify a repetition range, like so: {min[,max]}.  If you only set the min value then it is an exact count.

So what you have says match exactly 5 digits, no more and no less. Presumably, you'd want to instead match between 0 and 5 digits, assuming something like 8M is valid.  To do that, you set both values accordingly.

\d{0,5}

 

Link to comment
Share on other sites

Bonus fact: the default min value is 0 and the default max value is unbounded/infinity, so if you use that comma (to indicate it's not an {exact} count but a {min,max} range) then you can leave out the min value itself to get the same "up to X" count.

\d{,5}

 

Link to comment
Share on other sites

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.