regex Visual Studio代码段:将'PascalCase'转换为带空格的小写

wdebmtf2  于 2023-08-08  发布在  其他
关注(0)|答案(3)|浏览(119)

我正在尝试编写一个Visual Studio代码片段,将Pascal case转换为 * 小写空格 *。我快到了:

${1/([A-Z]*)([A-Z][a-z]+)/$1 ${2:/downcase} /g}

字符串
第1组匹配首字母缩略词,第2组匹配大写单词。
因此,如果占位符是MyUIDTest,则预期结果将是my UID test。但是现在我得到了my UID test(注意两边的空格)。
如果组1有匹配项,如何仅在组1后添加空格?我如何删除行尾的空格?

eulz3vhy

eulz3vhy1#

根据文档,${1:+ }表示“插入空格 iff 组1被捕获”:

${
  TM_SELECTED_TEXT
  /
    (?<=([A-Za-z])?)      # Lookbehind for and capture ($1) a preceding letter, if any,
    (?:                   # then match a "word" – either
      ([A-Z]+)(?![a-z])   # ($2) 1+ uppercase not followed by a lowercase
    |                     # or
      ([A-Z][a-z]+)       # ($3) an uppercase followed by 1+ lowercase.
    )                     #
  /                       # Replace each match with
    ${1:+\u0020}          # a space, iff group 1 is captured,
    ${2:/upcase}          # the uppercase version of group 2
    ${3:/downcase}        # and the lowercase version of group 3.
  /g                      # for all matches found.
}

字符串
由于$2$3是互斥的,因此同时使用它们总是会导致实际上只插入一个。
| 之后| After |
| --| ------------ |
| pascal case个| pascal case |
| my UID test个| my UID test |
| fo O ba R个| fo O ba R |
一个更精确(和详细)的版本是(注意,当以JSON字符串编写时,\b需要再次转义:"\\b"):

${
  TM_SELECTED_TEXT
  /
    (?<=                                    # Lookbehind for
      \b                                    # a word boundary followed by
      ([A-Z]+(?![a-z])|[A-Z][a-z]+)*        # 0+ "words"
                                            # and capture the last word, if any.
    )
    (?:([A-Z]+)(?![a-z])|([A-Z][a-z]+))
  /
    ${1:+\u0020}${2:/upcase}${3:/downcase}
  /g
}


我在这里使用的是TM_SELECTED_TEXT,但您可能希望根据需要更改它。

f3temu5u

f3temu5u2#

我认为要删除行尾的空格,您可以简单地在替换模式的末尾添加一个换行符,并且当您使用带有占位符“MyUIDTest”的片段时,它应该正确地生成“我的UID测试”,而没有任何额外的空格。

${1/(?:([A-Z]+)(?=[A-Z][a-z]))?([A-Z][a-z]+)/$1 ?${2:/downcase} :''/g}\n

字符串
此外,要仅在组1匹配时才在组1后添加空格,可以在替换模式中使用条件语句

${1/(?:([A-Z]+)(?=[A-Z][a-z]))?([A-Z][a-z]+)/$1 ?${2:/downcase} :''/g}

kq0g1dla

kq0g1dla3#

感谢@bobblebubble:

${
  TM_SELECTED_TEXT
  /                     # Match either
    (\w)?               # an optional group containing a word character 
    ([A-Z])             # then a group containing an uppercase
    (?=[a-z])           # which is followed by a lowercase
    |                   # or
    ([a-z])(?=[A-Z])    # vice versa.
  /                     #
    $1${1:+ }           # If group 1 was captured, put it back followed by a space.
    $3${3:+ }           # Do the same with group 3.
    ${2:/downcase}      # Finally, insert a lowercased version of group 2.
  /g
}

字符串

相关问题