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: Only allow certain format within string

I am using regex to validate input within a Google form, the input value should only contain the following formats . For example should work but or should not. I have tried to use the following regex

But this does not limit to the following input

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

>Solution :

You could shorten it to an alternation, and not that SSDC66765 has 5 digits so the quantifier can be {3,5} instead (or adjust to your needs)

^(?:SS(?:FW|DC)?|[HT]R)\d{3,5}$

The pattern matches:

  • ^ Start of string
  • (?: Non capture group
    • SS(?:FW|DC)? Match either SS SSFW or SSDC
    • | Or
    • [HT]R Match either HR or TR
  • ) Close non capture group
  • \d{3,5} Match 3 – 5 digits
  • $ End of string

Regex demo

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