我无法使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 => This Is A Title CASE String
- andThisOneIsCamelCASE => and This One Is 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 => my Variable 123
- my2Variables => my 2 Variables
- The3rdVariableIsHere => The 3 rdVariable Is Here
- 12345NumsAtTheStartIncludedToo => 12345 Nums At The Start Included Too