Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: matching for digits excluding some digits at a position

I have a string which has a pattern, say "xxx.xxx".

So first three are digits, then a . and then 3 more digits.

I want to check if first digit is always 3, and next 2 can be any digits except 30 and 40, rest can be anything.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My regex attempt is:

"3[^34][0-9]."

But it doesn’t seem to work. Which part here is wrong?

>Solution :

Try this: ^3(?!30|40)\d{2}\.\d{3}$

Test regex here: https://regex101.com/r/iCtKnB/1

^          matches the start of string
3          matches the number 3
(?!30|40)  checks if the next two numbers are not either 30 or 40
\d{2}      matches two digits if they are not 30 or 40
\.         matches period
\d{3}      matches the next three digits
$

Basically this will match any number of your pattern which begins with 3 not followed by 30 or 40 then a dot, followed by another three digits.

In your regex: "3[^34][0-9]."

  • 3 > This checks the number starts with 3
  • [^34] > then the Next digit is not 3 or 4
  • [0-9] > followed by one digits which can be 0 to 9
  • . > followed by any other char that is not white spaces

So your requirements were not being checked in this regex.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading