stretchr
stretchr

Reputation: 615

Java regex for preceding character

I am trying to construct a regex to check if a letter occurs in a string, it should be precede only one of 2 other characters. I can do this for one set of chars, but how do I do it for multiple sets in a string?

For example: C can only precede A or B, i.e. if C is found the next char can only be A or B. F can only precede D or E (F cannot precede A or B)

A or B, then C can occur D or E, then F can occur

How do I do this?

The following gives me an error:

String pattern = "([F][D|E])|([A][B|C])";
String test = "FEAC";
System.out.println(test.matches(pattern));

Upvotes: 1

Views: 822

Answers (1)

zx81
zx81

Reputation: 41838

Assuming the only allowable letters are A to F, you can use this:

^(?:C[AB]|F[DE]|[ABDEG-Z])+$

See the matches in the demo

Explanation

  • The anchors won't be necessary with the matches method, but explaining them for the raw version:
  • The ^ anchor asserts that we are at the beginning of the string
  • The $ anchor asserts that we are at the end of the string
  • Match C[AB] a C then an A or B, OR |
  • F[DE] an F then a D or E, OR |
  • [ABDEG-Z] one of these letters
  • + one or more times

Option: Allowing C and F at the end of the string

If you want to allow C or F at the end of the string, add this: |[CF]$ (one of several ways to do it)

The regex becomes:

^(?:C[AB]|F[DE]|[ABDEG-Z]|[CF]$)+$

In Java:

if (subjectString.matches("(?:C[AB]|F[DE]|[ABDEG-Z])+")) {
    // It matched!
  } 
else {  // nah, it didn't match...  
     } 

Upvotes: 2

Related Questions