简体   繁体   中英

RegEx for checking if capital letters exist in a string

I'm trying to extract then correct Capital letters found in the middle of a word.

I thought of this regex but it's not doing exactly what I want, it's only detecting parts of the wanted results .

[A-Z]([a-z][A-Z]?)+|[a-z]([A-Z][a-z]?)+

See here: https://www.regexplanet.com/share/index.html?share=yyyyd810vnr

Example :

Input:
Il CodiCe della mappa non deVe usCire dalla tomba.

The regex should identify : CodiCe , deVe , usCire .. in order to correct them to lowercase later ..

Input:
E il CodiCe per deCifrare la mappa si troVa a Volubilis, ViCino alle roVine romane in MaroCCo.

The regex should identify : CodiCe , deCifrare , troVa , Volubilis , ViCino ,roVine , MaroCCo

NOTE: Words at the beginning of the segment should be excluded.

You can use this regex which will match any word having at least one capital letter within, except this will ignore matching first word in any case.

\b(?<!^)(?=[a-z]*[A-Z])[a-zA-Z]+\b

Regex Demo

And here is a Java code that will give you the string where each word having capital letter will be converted to lowercase using matcher.appendReplacement

List<String> sentenceList = Arrays.asList("Il CodiCe della mappa non deVe usCire dalla tomba.",
        "E il CodiCe per deCifrare la mappa si troVa a Volubilis, ViCino alle roVine romane in MaroCCo.");
Pattern p = Pattern.compile("\\b(?<!^)(?=[a-z]*[A-Z])[a-zA-Z]+\\b");

sentenceList.forEach(x -> {
    System.out.println("Input: " + x);
    Matcher m = p.matcher(x);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, m.group().toLowerCase());
    }
    m.appendTail(sb);
    System.out.println("Lowercased string: " + sb);
    System.out.println();
    });

Prints,

Input: Il CodiCe della mappa non deVe usCire dalla tomba.
Lowercased string: Il codice della mappa non deve uscire dalla tomba.

Input: E il CodiCe per deCifrare la mappa si troVa a Volubilis, ViCino alle roVine romane in MaroCCo.
Lowercased string: E il codice per decifrare la mappa si trova a volubilis, vicino alle rovine romane in marocco.

How about replacing the string with the first letter concatenated with the remainder in lowercase:

String input = "E il CodiCe per deCifrare la mappa si troVa a Volubilis, ViCino alle roVine romane in MaroCCo.";
String[] parts = input.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0; i < parts.length; ++i) {
    if (i > 0) sb.append(" ");
    sb.append(parts[i].substring(0, 1)).append(parts[i].substring(1).toLowerCase());
}

System.out.println(sb);

E il Codice per decifrare la mappa si trova a Volubilis, Vicino alle rovine romane in Marocco.

This regex does find all those words as well.

\b\w+[A-Z]+\w+\b

https://regex101.com/r/5lyTG3/4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM