What I need to do is to determine whether a word consists of letters except certain letters. For example I need to test whether a word consists of the letters from the English alphabet except letters: I, V and X.
Currently I have this long regex for the simple task above:
Pattern pattern = Pattern.compile("[ABCDEFGHJKLMNOPQRSTUWYZ]+");
Any of you know any shorthand way of excluding certain letters from a Java regex? Thanks.
You can use the
&& operator to create a compound character class using subtraction:
String regex = "[A-Z&&[^IVX]]+";
You could simply specify character ranges inside your character class:
Just use a negative lookahead in your pattern.
Pattern pattern = Pattern.compile("^(?:(?![IVX])[A-Z])+$");
[A-Z&&[^IVX]]+ to exclude certain characters from the
A-Z range - see Pattern