Help constructing regex

java, regex, string-matching

Solution

Try this regular expression:

^([+-]|VC)?[A-Z]{4}$

Problem

I need to know if a string matches a number of different criterias. I'm trying to solve this by using a regular expression and then see if it matches (in Java: str.matches(myRegex);), but I can't get it right. The criterias are as follows: - The string to match is constructed of 4 letters, [A-Z] - It may be preceeded (but not necessarily) by one of "-", "+" or "VC" - It shall only match strings containing exactly 4 letters (and the possibly preceeding characters) Examples: - "SHSN" -> match - "+SHRA" -> match - "VCSHRA" -> match - "CAVOK" -> no match - "-+SHSN" -> no match Is this possible to do in one single regex? Or should it be done in code or a combination of the two? Thanks, Linus

Original source