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 for checking specific rule in string

I cannot figure out how to make this RegEx syntax work.
https://regex101.com/r/Zcxjtn/1

I would like to check whether a string is valid or not.
Rules:

  • The string must consist of 3 capital letters [A-Z]
  • If the string is longer than each 3 capital letter blocks must be seperated by a semicolon (;) only
  • The string must not start and end with a seperator (;)
  • optional: whitespaces are allowed between seperator and next 3-letter sub-string

examples of valid strings:

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

AAA;BBB

AAA; BBB

AAA

examples of invalid strings:

;AAA

AAA;BBB;

123;AAA

>Solution :

The string must consist of 3 capital letters [A-Z]

  • [A-Z] matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)

  • {3} matches the previous token exactly 3 times

Put together you get

  1. [A-Z]{3}

If the string is longer than each 3 capital letter blocks must be seperated by a semicolon (;) only

Make 1. required in the beginning as ^[A-Z]{3} followed by another group that occurs 0 or more times until the end of the input ( )*$ containing a leading ; and 1. from above, so (;[A-Z]{3})*$.

Put together you get

  1. ^[A-Z]{3}(;[A-Z]{3})*$

The string must not start and end with a seperator (;)

Already covered by 2.

optional: whitespaces are allowed between seperator and next 3-letter sub-string

Add a white-space \s that occurs 0 or more times *, so \s*.

  • \s matches any whitespace character (equivalent to [\r\n\t\f\v ])

Put to the correct location in the regex you get

^[A-Z]{3}(;\s*[A-Z]{3})*$

See: https://regex101.com/r/hZ5l6a/1

If you would like to capture only letters, add capture groups ( ) and mark some groups as non-capturing groups (?: )

Example:
^([A-Z]{3})(:?;\s*([A-Z]{3}))*$

See: https://regex101.com/r/NlDTfq/1

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