Regex用小写字母替换大写。

时间:2023-01-15 00:10:05

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

我试着用正则表达式替换大写字母。这

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

becomes

就变成了

earth:   1,
mercury: 0.2408467,
venus:   0.61519726,

in Sublime Text. How can I downcase letters only in words that contain both lower and uppercase letters? So that it affects venUs and not VENUS.

在崇高的文本。我怎样才能把字母只包含小写字母和大写字母呢?所以它影响的是金星而不是金星。

5 个解决方案

#1


224  

You may:

你可以:

Find: (\w) Replace With: \L$1

查找:(\w)替换为:\L$1。

Or select the text, ctrl+K+L.

或者选择文本,ctrl+K+L。

#2


78  

I figured this might come in handy for others as well :

我想这对其他人来说也很有用:

find:

发现:

  • ([A-Z])(.*)
  • ([a - z])(*)。

replace:

替换:

  • \L$1$2 --> will convert $1 and $2 to lowercase
    BUT
  • \L$1$2—>将$1和$2转换为小写,但是。
  • \l$1$2 --> will only convert $1 to lowercase and leave $2 as it is
  • \l$1$2—>只会将$1转换为小写,并留下$2。

The same goes for uppercase with \U and \u

同样的情况也适用于\U和\U。

#3


54  

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:

在使用regex like [A-Z]进行搜索之前,您应该按下区分大小写的按钮(或Alt+C)(如leemour很好地建议在被接受的答案中编辑)。为了澄清一下,我留下了一些其他的例子:

  1. Capitalize words
    • Find: (\s)([a-z]) (\s also matches new lines, i.e. "venuS" => "VenuS")
    • 找到:(\s)([a-z]) (\s也匹配新行,即。“金星”= >“金星”)
    • Replace: $1\u$2
    • 替换:$ 1 \ u 2美元
  2. 大写的单词找到:(\s)([a-z]) (\s也匹配新行,即。“金星”=>“金星”)替换:$1\ $2。
  3. Uncapitalize words
    • Find: (\s)([A-Z])
    • 发现:(\ s)([a - z])
    • Replace: $1\l$2
    • 替换:$ 1 \ l 2美元
  4. (A-Z)替换:$1\l$2。
  5. Remove camel case (e.g. cAmelCAse => camelcAse => camelcase)
    • Find: ([a-z])([A-Z])
    • 发现:([a - z])([a - z])
    • Replace: $1\l$2
    • 替换:$ 1 \ l 2美元
  6. 删除驼峰(例如cAmelCAse => cAmelCAse => cAmelCAse): ([a-z])替换:$1\l$2。
  7. Lowercase letters within words (e.g. LowerCASe => Lowercase)
    • Find: (\w)([A-Z]+)
    • 发现:(\ w)([a - z]+)
    • Replace: $1\L$2
    • 替换:$ 1 \ L 2美元
    • Alternate Replace: \L$0
    • 备用替换:\ L $ 0
  8. 小写字母(如小写=>小写)查找:(\w)([A-Z]+)替换:$1\L$2备用替换:\L$0。
  9. Uppercase letters within words (e.g. upperCASe => uPPERCASE)
    • Find: (\w)([A-Z]+)
    • 发现:(\ w)([a - z]+)
    • Replace: $1\U$2
    • 替换:$ 1 \ U 2美元
  10. 大写的大写字母(例如大写字母=>大写)查找:(\w)([A-Z]+)替换:$1\ $2。
  11. Uppercase previous (e.g. upperCase => UPPERCase)
    • Find: (\w+)([A-Z])
    • 发现:(\ w +)([a - z])
    • Replace: \U$1$2
    • 替换:\ U $ 1 $ 2
  12. 大写字母(如大写字母=>大写)查找:(\w+)([A-Z])替换:\U$1$2。
  13. Lowercase previous (e.g. LOWERCase => lowerCase)
    • Find: (\w+)([A-Z])
    • 发现:(\ w +)([a - z])
    • Replace: \L$1$2
    • 替换:\ L $ 1 $ 2
  14. 之前的小写字母(例如小写的>小写)找到:(\w+)([A-Z])替换:\L$1$2。
  15. Uppercase the rest (e.g. upperCase => upperCASE)
    • Find: ([A-Z])(\w+)
    • 发现:([a - z])(\ w +)
    • Replace: $1\U$2
    • 替换:$ 1 \ U 2美元
  16. 其余的(如大写字母=>大写)查找:([A-Z])(\w+)替换:$1\ $2。
  17. Lowercase the rest (e.g. lOWERCASE => lOwercase)
    • Find: ([A-Z])(\w+)
    • 发现:([a - z])(\ w +)
    • Replace: $1\L$2
    • 替换:$ 1 \ L 2美元
  18. 其余的(例如小写的=>小写)查找:([A-Z])(\w+)替换:$1\L$2。
  19. Shift-right-uppercase (e.g. Case => cAse => caSe => casE)
    • Find: ([a-z\s])([A-Z])(\w)
    • 发现:([a - z \ s])([a - z])(\ w)
    • Replace: $1\l$2\u$3
    • 替换:$ 1 \ \ u l 2美元3美元
  20. Shift-right-uppercase(例如情况= >情况= > = >案件)发现:([a - z \ s])([a - z])(\ w)取代:1美元\ \ u l 2美元3美元
  21. Shift-left-uppercase (e.g. CasE => CaSe => CAse => Case)
    • Find: (\w)([A-Z])([a-z\s])
    • 发现:(\ w)([a - z])([a - z \ s])
    • Replace: \u$1\l$2$3
    • 替换:\ u $ 1 \ l $ 2 $ 3
  22. Shift-left-uppercase(例如情况= >情况= > = >案件)发现:(\ w)([a - z])([a - z \ s])取代:\ 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:

关于这个问题(至少一个大写和一个小写字母的匹配词,并使它们小写),leemour的答案是正确的。为了澄清,如果只有一个组可以替换,您可以使用?:在内部组(即非捕获组)或避免创建它们:

  • Find: ((?:[a-z][A-Z]+)|(?:[A-Z]+[a-z])) OR ([a-z][A-Z]+|[A-Z]+[a-z])
  • 发现:((?):[a - z][a - z]+)|(?:[a - z]+[a - z]))或([a - z][a - z]+ |[a - z]+[a - z])
  • Replace: \L$1
  • 替换:\ L 1美元

2016-06-23 Edit

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

通过编辑这个答案,泰勒提出了第4条的另一种表达方式:

  • (\B)([A-Z]+)
  • (\ 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.

根据文档,\B将寻找一个不在单词边界的字符(即不是在开始时,而不是在结尾)。您可以使用Replace All按钮,它所做的事情与find表达式相同(\w)([A-Z]+)。

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).

但是,\B的缺点是它不允许单个替换,可能是由于find的“not boundary”限制(如果您知道确切的原因,请进行编辑)。

#4


0  

Try this

试试这个

  • Find: ([A-Z])([A-Z]+)\b
  • 发现:([a - z])([a - z]+)\ b
  • Replace: $1\L$2
  • 替换:$ 1 \ L 2美元

Make sure case sensitivity is on (Alt + C)

确保区分大小写(Alt + C)

#5


0  

Regular expression

正则表达式

Find:\w+

发现:\ w +

Replace:\L$0

替换:\ L $ 0

Sublime Text uses the Perl Compatible Regular Expressions (PCRE) engine from the Boost library to power regular expressions in search panels.

卓越的文本使用来自Boost库的Perl兼容的正则表达式(PCRE)引擎来在搜索面板中提供正则表达式。

\L Converts everything up to lowercase

\L将所有内容转换为小写。

$0 Capture groups

0美元捕捉组

#1


224  

You may:

你可以:

Find: (\w) Replace With: \L$1

查找:(\w)替换为:\L$1。

Or select the text, ctrl+K+L.

或者选择文本,ctrl+K+L。

#2


78  

I figured this might come in handy for others as well :

我想这对其他人来说也很有用:

find:

发现:

  • ([A-Z])(.*)
  • ([a - z])(*)。

replace:

替换:

  • \L$1$2 --> will convert $1 and $2 to lowercase
    BUT
  • \L$1$2—>将$1和$2转换为小写,但是。
  • \l$1$2 --> will only convert $1 to lowercase and leave $2 as it is
  • \l$1$2—>只会将$1转换为小写,并留下$2。

The same goes for uppercase with \U and \u

同样的情况也适用于\U和\U。

#3


54  

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:

在使用regex like [A-Z]进行搜索之前,您应该按下区分大小写的按钮(或Alt+C)(如leemour很好地建议在被接受的答案中编辑)。为了澄清一下,我留下了一些其他的例子:

  1. Capitalize words
    • Find: (\s)([a-z]) (\s also matches new lines, i.e. "venuS" => "VenuS")
    • 找到:(\s)([a-z]) (\s也匹配新行,即。“金星”= >“金星”)
    • Replace: $1\u$2
    • 替换:$ 1 \ u 2美元
  2. 大写的单词找到:(\s)([a-z]) (\s也匹配新行,即。“金星”=>“金星”)替换:$1\ $2。
  3. Uncapitalize words
    • Find: (\s)([A-Z])
    • 发现:(\ s)([a - z])
    • Replace: $1\l$2
    • 替换:$ 1 \ l 2美元
  4. (A-Z)替换:$1\l$2。
  5. Remove camel case (e.g. cAmelCAse => camelcAse => camelcase)
    • Find: ([a-z])([A-Z])
    • 发现:([a - z])([a - z])
    • Replace: $1\l$2
    • 替换:$ 1 \ l 2美元
  6. 删除驼峰(例如cAmelCAse => cAmelCAse => cAmelCAse): ([a-z])替换:$1\l$2。
  7. Lowercase letters within words (e.g. LowerCASe => Lowercase)
    • Find: (\w)([A-Z]+)
    • 发现:(\ w)([a - z]+)
    • Replace: $1\L$2
    • 替换:$ 1 \ L 2美元
    • Alternate Replace: \L$0
    • 备用替换:\ L $ 0
  8. 小写字母(如小写=>小写)查找:(\w)([A-Z]+)替换:$1\L$2备用替换:\L$0。
  9. Uppercase letters within words (e.g. upperCASe => uPPERCASE)
    • Find: (\w)([A-Z]+)
    • 发现:(\ w)([a - z]+)
    • Replace: $1\U$2
    • 替换:$ 1 \ U 2美元
  10. 大写的大写字母(例如大写字母=>大写)查找:(\w)([A-Z]+)替换:$1\ $2。
  11. Uppercase previous (e.g. upperCase => UPPERCase)
    • Find: (\w+)([A-Z])
    • 发现:(\ w +)([a - z])
    • Replace: \U$1$2
    • 替换:\ U $ 1 $ 2
  12. 大写字母(如大写字母=>大写)查找:(\w+)([A-Z])替换:\U$1$2。
  13. Lowercase previous (e.g. LOWERCase => lowerCase)
    • Find: (\w+)([A-Z])
    • 发现:(\ w +)([a - z])
    • Replace: \L$1$2
    • 替换:\ L $ 1 $ 2
  14. 之前的小写字母(例如小写的>小写)找到:(\w+)([A-Z])替换:\L$1$2。
  15. Uppercase the rest (e.g. upperCase => upperCASE)
    • Find: ([A-Z])(\w+)
    • 发现:([a - z])(\ w +)
    • Replace: $1\U$2
    • 替换:$ 1 \ U 2美元
  16. 其余的(如大写字母=>大写)查找:([A-Z])(\w+)替换:$1\ $2。
  17. Lowercase the rest (e.g. lOWERCASE => lOwercase)
    • Find: ([A-Z])(\w+)
    • 发现:([a - z])(\ w +)
    • Replace: $1\L$2
    • 替换:$ 1 \ L 2美元
  18. 其余的(例如小写的=>小写)查找:([A-Z])(\w+)替换:$1\L$2。
  19. Shift-right-uppercase (e.g. Case => cAse => caSe => casE)
    • Find: ([a-z\s])([A-Z])(\w)
    • 发现:([a - z \ s])([a - z])(\ w)
    • Replace: $1\l$2\u$3
    • 替换:$ 1 \ \ u l 2美元3美元
  20. Shift-right-uppercase(例如情况= >情况= > = >案件)发现:([a - z \ s])([a - z])(\ w)取代:1美元\ \ u l 2美元3美元
  21. Shift-left-uppercase (e.g. CasE => CaSe => CAse => Case)
    • Find: (\w)([A-Z])([a-z\s])
    • 发现:(\ w)([a - z])([a - z \ s])
    • Replace: \u$1\l$2$3
    • 替换:\ u $ 1 \ l $ 2 $ 3
  22. Shift-left-uppercase(例如情况= >情况= > = >案件)发现:(\ w)([a - z])([a - z \ s])取代:\ 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:

关于这个问题(至少一个大写和一个小写字母的匹配词,并使它们小写),leemour的答案是正确的。为了澄清,如果只有一个组可以替换,您可以使用?:在内部组(即非捕获组)或避免创建它们:

  • Find: ((?:[a-z][A-Z]+)|(?:[A-Z]+[a-z])) OR ([a-z][A-Z]+|[A-Z]+[a-z])
  • 发现:((?):[a - z][a - z]+)|(?:[a - z]+[a - z]))或([a - z][a - z]+ |[a - z]+[a - z])
  • Replace: \L$1
  • 替换:\ L 1美元

2016-06-23 Edit

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

通过编辑这个答案,泰勒提出了第4条的另一种表达方式:

  • (\B)([A-Z]+)
  • (\ 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.

根据文档,\B将寻找一个不在单词边界的字符(即不是在开始时,而不是在结尾)。您可以使用Replace All按钮,它所做的事情与find表达式相同(\w)([A-Z]+)。

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).

但是,\B的缺点是它不允许单个替换,可能是由于find的“not boundary”限制(如果您知道确切的原因,请进行编辑)。

#4


0  

Try this

试试这个

  • Find: ([A-Z])([A-Z]+)\b
  • 发现:([a - z])([a - z]+)\ b
  • Replace: $1\L$2
  • 替换:$ 1 \ L 2美元

Make sure case sensitivity is on (Alt + C)

确保区分大小写(Alt + C)

#5


0  

Regular expression

正则表达式

Find:\w+

发现:\ w +

Replace:\L$0

替换:\ L $ 0

Sublime Text uses the Perl Compatible Regular Expressions (PCRE) engine from the Boost library to power regular expressions in search panels.

卓越的文本使用来自Boost库的Perl兼容的正则表达式(PCRE)引擎来在搜索面板中提供正则表达式。

\L Converts everything up to lowercase

\L将所有内容转换为小写。

$0 Capture groups

0美元捕捉组