How to match any uppercase letter followed by the corresponding lowercase letter?

I have a requirement that a name should not start with three identical letters ignoring their case. The name begins with an uppercase letter followed by lowercase letters.

Basically, I could convert the entire name to uppercase and then match the regular expression, for example (\p{Lu})\1{3,}.* .

But I was wondering if there is a regular expression that meets the above requirements, and does not need to preprocess the string that needs to be matched. So, what regular expression can be used to match strings like Aa , Dd or Uu without explicitly specifying any possible combination?

EDIT:
I accepted the answer of Marcos. I just needed to fix it to work with names of lengths 1 and 2 and bind it at the beginning. So the actual regex for my use case ^(\p{Lu})(\p{Ll}?$|(?=\p{Ll}{2})(?i)(?!(\1){2})) .

I also supported the answers of Eugene and sp00m for helping me learn a lesson in regular expressions.

Thank you for your efforts.

+7
source share
6 answers

I admit that I stood on the shoulders of the giants (other posters here), but this solution really 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:

  • match for one upper char cabinet in front;
  • The following statement is the bottom two characters:
  • the other view that affirms these two characters is not the same (ignoring the case) as the first.

Output:

  Aba: true
 ABa: false
 aba: false
 aBa: false
 Aaa: false
 Aab: true 
+3
source

to 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)); 

Exit

 false false false true 
+2
source

This matches any uppercase letter followed by the same letter, upper or lower:

 ([AZ])(?i)\1 

This matches any uppercase letter followed by the same letter, but always lowercase:

 ([AZ])(?!\1)(?i)\1 

For example, in Java,

 String pattern = "([AZ])(?!\\1)(?i)\\1"; System.out.println("AA".matches(pattern)); System.out.println("aa".matches(pattern)); System.out.println("aA".matches(pattern)); System.out.println("Aa".matches(pattern)); 

Print

 false false false true 
+2
source

Evgeny Dorofeev's solution works (+1), but it can be made simpler using just viewing

 (\\p{Lu})(?=\\p{Ll})(?i)\\1 

(\\p{Lu}) matches the uppercase character and stores it in \\1

(?=\\p{Ll}) is a positive lookback statement that ensures that the next character is a lowercase letter.

(?i) is a built-in modifier that provides independence from the case.

\\1 matches the capital letter of the first part (but now it is independent of the modifier in front).

Check this:

 String[] TestInput = { "foobar", "Aal", "TTest" }; Pattern p = Pattern.compile("(\\p{Lu})(?=\\p{Ll})(?i)\\1"); for (String t : TestInput) { Matcher m = p.matcher(t); if (m.find()) { System.out.println(t + " ==> " + true); } else { System.out.println(t + " ==> " + false); } } 

Output:

foobar ==> false
Aal ==> true
TTest ==> false

+2
source

I have a requirement that a name should not start with three identical letters ignoring their case.

You must use the case insensitive option: (?i)

and "catch-all" \w for example: (?i)(\w)\1{2,}.*

or just [az] for example: (?i)([az])\1{2,}.*

+1
source

It might make sense to use separate checks for different requirements, especially because the lists of requirements increase over time.

Your requirements, as described, are as follows:

The name should not begin with 3 identical letters, ignoring their case

and

The name begins with an uppercase letter followed by lowercase letters.

Performing a separate check for each (as described in other posts) also allows you to tell the user the correct error messages describing what is actually wrong. And it is certainly more readable.

0
source

All Articles