DanRz Posted April 17, 2023 Share Posted April 17, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/316140-up-to-6-numbers-followed-by-a-letter/ Share on other sites More sharing options...
Solution kicken Posted April 17, 2023 Solution Share Posted April 17, 2023 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} Quote Link to comment https://forums.phpfreaks.com/topic/316140-up-to-6-numbers-followed-by-a-letter/#findComment-1607391 Share on other sites More sharing options...
requinix Posted April 17, 2023 Share Posted April 17, 2023 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} Quote Link to comment https://forums.phpfreaks.com/topic/316140-up-to-6-numbers-followed-by-a-letter/#findComment-1607392 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.