I try to make a string pattern that can contain UTF-8 characters (öäå, etc) and match the following criteria in Java (1.7);
- Must start with # or @
- Must be lower-case
- Can contain – or _ (minus and underscore), no other special char
- Can contain digits (0-9)
- Min 3 and maximum 15 characters long
What I have at the moment that works but are missing many of the criteria.
“#p{javaLowerCase}+”
I don’t know how to complete and add the rest of the criteria. How would a regex expression look like that can accommodate the criteria I have?
Advertisement
Answer
This translates into a regex in a relatively simple way:
"[#@][\p{javaLowerCase}\d_-]{2,14}"
This translates the “Min 3 and maximum 15 characters” inclusive of the #
or @
at the beginning. If these should not be counted, change the suffix to {3,15}
.