Help creating a regex

I need to know if a string matches several different criteria. I try to solve this using a regex and then see if it matches (in Java: str.matches (myRegex);), but I can't figure it out correctly.

The criteria are as follows:

  • Corresponding line is constructed of 4 letters, [AZ]
  • Perhaps it may precede (but not necessarily) one of the "-", "+" or "VC"
  • It should match only strings containing exactly 4 letters (and possibly the preceding characters)

Examples:

  • "SHSN" → match
  • "+ SHRA" → match
  • "VCSHRA" → match
  • "CAVOK" → no match
  • "- + SHSN" → no match

Can this be done in one regex? Or should it be done in code or in a combination of the two?

Thanks,

Linus

+4
source share
1 answer

Try this regex:

^([+-]|VC)?[AZ]{4}$ 
+7
source

Source: https://habr.com/ru/post/1299879/


All Articles