Regex replace uppercase with lowercase letters

前端 未结 6 1607
孤独总比滥情好
孤独总比滥情好 2020-12-04 05:14

I\'m trying to replace uppercase letters with corresponding lowercase letters using regex. So that

EarTH:   1,
MerCury: 0.2408467,
venuS:   0.61519726,
         


        
6条回答
  •  無奈伤痛
    2020-12-04 05:31

    Before searching with regex like [A-Z], you should press the case sensitive button (or Alt+C) (as leemour nicely suggested to be edited in the accepted answer). Just to be clear, I'm leaving a few other examples:

    1. Capitalize words
      • Find: (\s)([a-z]) (\s also matches new lines, i.e. "venuS" => "VenuS")
      • Replace: $1\u$2
    2. Uncapitalize words
      • Find: (\s)([A-Z])
      • Replace: $1\l$2
    3. Remove camel case (e.g. cAmelCAse => camelcAse => camelcase)
      • Find: ([a-z])([A-Z])
      • Replace: $1\l$2
    4. Lowercase letters within words (e.g. LowerCASe => Lowercase)
      • Find: (\w)([A-Z]+)
      • Replace: $1\L$2
      • Alternate Replace: \L$0
    5. Uppercase letters within words (e.g. upperCASe => uPPERCASE)
      • Find: (\w)([A-Z]+)
      • Replace: $1\U$2
    6. Uppercase previous (e.g. upperCase => UPPERCase)
      • Find: (\w+)([A-Z])
      • Replace: \U$1$2
    7. Lowercase previous (e.g. LOWERCase => lowerCase)
      • Find: (\w+)([A-Z])
      • Replace: \L$1$2
    8. Uppercase the rest (e.g. upperCase => upperCASE)
      • Find: ([A-Z])(\w+)
      • Replace: $1\U$2
    9. Lowercase the rest (e.g. lOWERCASE => lOwercase)
      • Find: ([A-Z])(\w+)
      • Replace: $1\L$2
    10. Shift-right-uppercase (e.g. Case => cAse => caSe => casE)
      • Find: ([a-z\s])([A-Z])(\w)
      • Replace: $1\l$2\u$3
    11. Shift-left-uppercase (e.g. CasE => CaSe => CAse => Case)
      • Find: (\w)([A-Z])([a-z\s])
      • Replace: \u$1\l$2$3

    Regarding the question (match words with at least one uppercase and one lowercase letter and make them lowercase), leemour's comment-answer is the right answer. Just to clarify, if there is only one group to replace, you can just use ?: in the inner groups (i.e. non capture groups) or avoid creating them at all:

    • Find: ((?:[a-z][A-Z]+)|(?:[A-Z]+[a-z])) OR ([a-z][A-Z]+|[A-Z]+[a-z])
    • Replace: \L$1

    2016-06-23 Edit

    Tyler suggested by editing this answer an alternate find expression for #4:

    • (\B)([A-Z]+)

    According to the documentation, \B will look for a character that is not at the word's boundary (i.e. not at the beginning and not at the end). You can use the Replace All button and it does the exact same thing as if you had (\w)([A-Z]+) as the find expression.

    However, the downside of \B is that it does not allow single replacements, perhaps due to the find's "not boundary" restriction (please do edit this if you know the exact reason).

提交回复
热议问题