我无法让 aix 的解决方案正常工作(它也不能在 RegExr 上工作),所以我想出了我自己的,我已经测试过并且似乎完全符合您的要求:
((^[a-z]+)|([A-Z]{1}[a-z]+)|([A-Z]+(?=([A-Z][a-z])|($))))
这是一个使用它的例子:
; Regex Breakdown: This will match against each word in Camel and Pascal case strings, while properly handling acrynoms.
; (^[a-z]+) Match against any lower-case letters at the start of the string.
; ([A-Z]{1}[a-z]+) Match against Title case words (one upper case followed by lower case letters).
; ([A-Z]+(?=([A-Z][a-z])|($))) Match against multiple consecutive upper-case letters, leaving the last upper case letter out the match if it is followed by lower case letters, and including it if it's followed by the end of the string.
newString := RegExReplace(oldCamelOrPascalString, "((^[a-z]+)|([A-Z]{1}[a-z]+)|([A-Z]+(?=([A-Z][a-z])|($))))", "$1 ")
newString := Trim(newString)
在这里,我用空格分隔每个单词,所以这里有一些如何转换字符串的示例:
- ThisIsATitleCASEString => 这是标题 CASE 字符串
- andThisOneIsCamelCASE => 这个是 Camel CASE
上面的这个解决方案可以满足原始帖子的要求,但我还需要一个正则表达式来查找包含数字的骆驼和帕斯卡字符串,所以我还想出了这个变体来包含数字:
((^[a-z]+)|([0-9]+)|([A-Z]{1}[a-z]+)|([A-Z]+(?=([A-Z][a-z])|($)|([0-9]))))
以及使用它的一个例子:
; Regex Breakdown: This will match against each word in Camel and Pascal case strings, while properly handling acrynoms and including numbers.
; (^[a-z]+) Match against any lower-case letters at the start of the command.
; ([0-9]+) Match against one or more consecutive numbers (anywhere in the string, including at the start).
; ([A-Z]{1}[a-z]+) Match against Title case words (one upper case followed by lower case letters).
; ([A-Z]+(?=([A-Z][a-z])|($)|([0-9]))) Match against multiple consecutive upper-case letters, leaving the last upper case letter out the match if it is followed by lower case letters, and including it if it's followed by the end of the string or a number.
newString := RegExReplace(oldCamelOrPascalString, "((^[a-z]+)|([0-9]+)|([A-Z]{1}[a-z]+)|([A-Z]+(?=([A-Z][a-z])|($)|([0-9]))))", "$1 ")
newString := Trim(newString)
以下是一些示例,说明如何使用此正则表达式转换带有数字的字符串:
- myVariable123 => 我的变量 123
- my2Variables => 我的 2 个变量
- The3rdVariableIsHere => 第 3 个 rdVariable 在这里
- 12345NumsAtTheStartIncludedToo => 12345 开头的数字也包括在内