I have a requirement that says a name must not start with 3 identical letters ignoring their case. A name starts with an upper case letter followed by lower case letters.
Basically I could convert the whole name to upper case and then match with a regex like (\p{Lu})\1{3,}.*
.
But I was wondering if there exists a regex that matches the above requirements and does not need any preprocessing of the string to be matched. So what regex can I use to match strings like Aa
, Dd
or Uu
without explicitly specifiying any possible combination?
EDIT:
I accepted Markos answer. I just needed to fix it to work with names of length 1 and two and anchor it at the beginning. So the actual regex for my use case is ^(\p{Lu})(\p{Ll}?$|(?=\p{Ll}{2})(?i)(?!(\1){2}))
.
I also upvoted the answers of Evgeniy and sp00m for helping me to learn a lesson in regexes.
Thanks for your efforts.
Using the lower() method One way to convert all the upper case letters into lower case using the inbuilt method lower() of the string library. This method converts all the characters present in a string to lowercase regardless of the character is uppercase or lowercase.
Uppercase characters (A-Z) Lowercase characters (a-z) Digits (0-9)
Alternatively known as caps and capital, and sometimes abbreviated as UC, uppercase is a typeface of larger characters. For example, typing a, b, and c shows lowercase, and typing A, B, and C shows uppercase. To type in uppercase, you can use either the Caps Lock key or the Shift key on the keyboard.
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.
I admit to rising on the shoulders of giants (the other posters here), but this solution actually works for your use case:
final String[] strings = { "Aba", "ABa", "aba", "aBa", "Aaa", "Aab" };
final Pattern p = Pattern.compile("(\\p{Lu})(?=\\p{Ll}{2})(?i)(?!(\\1){2})");
for (String s : strings) System.out.println(s + ": " + p.matcher(s).find());
Now we have:
Output:
Aba: true ABa: false aba: false aBa: false Aaa: false Aab: true
try
String regex = "(?i)(.)(?=\\p{javaLowerCase})(?<=\\p{javaUpperCase})\\1";
System.out.println("dD".matches(regex));
System.out.println("dd".matches(regex));
System.out.println("DD".matches(regex));
System.out.println("Dd".matches(regex));
output
false
false
false
true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With