Home > Mobile >  RegEx for checking specific rule in string
RegEx for checking specific rule in string

Time:07-30

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:

AAA;BBB

AAA; BBB

AAA

examples of invalid strings:

;AAA

AAA;BBB;

123;AAA

CodePudding user response:

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

CodePudding user response:

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and bonsai false otherwise.

  • Related