片段正则表达式:匹配任意数量的组并转换为 CamelCase [英] Snippet regex: match arbitrary number of groups and transform to CamelCase

查看:80
本文介绍了片段正则表达式:匹配任意数量的组并转换为 CamelCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编写的 Visual Studio 代码片段中,我想将蛇形大小写字符串转换为驼色大小写.

In a Visual Studio Code Snippet I'm writing I want to convert a snake case string to camel case.

docs 我知道语法是

'${' var '/' regex '/' (format | text)+ '/' options '}'

所以我想出了这个:

${TM_FILENAME_BASE/([a-z])([a-z]*)_+([a-z])([a-z]*)_+/${1:/upcase}$2${3:/upcase}$4/}

然而,此代码仅适用于具有 2 个元素的字符串(例如carrot_cake"),而我想处理具有任意数量元素的字符串(blueberry_pie_with_a_cup_of_coffee").

This code however works only for strings with 2 elements (for exemple "carrot_cake") while I would like to process strings with arbitrary number of elements ("blueberry_pie_with_a_cup_of_coffee").

我猜在 'regex''format' 中需要某种递归,但我不知道该怎么做.

I guess that some kind of recursion is needed in 'regex' and 'format', but I have no idea of what to do.

如何匹配任意数量的模式出现?

How does one match arbitrary number of pattern occurrences?

推荐答案

UPDATE

vscode v1.58 添加了 /camelcase 转换.这使您的用例现在变得非常简单.

vscode v1.58 is adding a /camelcase transform. which makes your use case very easy now.

${1/(.)(.*)/${1:/upcase}${2:/camelcase}/}"

blueberry_pie_with_a_cup_of_coffeeBlueberryPieWithACupOfCoffee

"${1/(.*)/${1:/camelcase}/}"//捕获整个内容

blueberry_pie_with_a_cup_of_coffeeblueberryPieWithACupOfCoffee

vscode 的实现使用这个正则表达式:[a-zA-Z0-9] 所以除了这些字符之外的任何东西都将被视为分隔符 - 以及下面的单词"将大写.

vscode's implementation uses this regex: [a-zA-Z0-9] so anything other than those characters will be seen as separators - and the following "word" will be capitalized.

上一个答案::

转换任意数量的_"将单词分隔成 CamelCase 试试:

To transform an arbitrary number of "_" separated words into CamelCase try:

编辑:2018 年 10 月(但截至 2020 年 2 月尚未添加到代码段语法文档中)vscode 添加了 /pascalcase 转换,请参阅 提交.我修改了下面的代码以使用 /pascalcase 转换.它仅适用于 some_file =>SomeFile 类型的 CamelCase.

EDIT: In October, 2018 (but not yet added to snippet grammar documentation as of February, 2020) vscode added the /pascalcase transform, see commit. I have modified the code below to use the /pascalcase transform. It only works for the some_file => SomeFile type of CamelCase though.

但是它可以使用许多字符作为分隔符,这些都可以:

But it works with many characters as separators, these all work:

blueberry_pie_with_a_cup_of_coffee
blueberry-pie-with-a-cup-of-coffee
blueberry-pie-with_a-cup-of_coffee
blueberry-pie-with.a-cup-of.coffee
blueberry*pie-with.a*cup-of.coffee
blueberry*pie@with.a*cup1of.coffee
blueberry*pie@with.a*cup1of.coffee


"camelCase": {
    "prefix": "_cc",
    "body": [
            // "${TM_FILENAME_BASE/([a-z]*)_+([a-z]*)/${1:/capitalize}${2:/capitalize}/g}"

            "${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}"
        ],
        "description": "Transform to camel case"
    },

carrot_cake.txt ->胡萝卜蛋糕

blueberry_pie_with_a_cup_of_coffee.js ->BlueberryPieWithACupOfCoffee

[我假设 CamelCase 是你想要的形式,还有其他的,比如 camelCase.]

[I assume CamelCase is the form you want, there are others, such as camelCase.]

对于camelCase:

For camelCase:

"${TM_FILENAME_BASE/([a-z]*)[-@_.*0-9]+([a-z]*)/$1${2:/capitalize}/g}"

将所需的分隔符列表放在 [-@_.*0-9]+ 部分.+ 量词允许您使用 carrot--cake 例如 - 多个分隔符字之间.感谢使用正则表达式的 [list the separators] 部分的其他答案.

put your desired list of separators in the [-@_.*0-9]+ part. The + quantifier allows you to use carrot--cake for example - multiple separators between words. Thanks to the other answer for using the [list the separators] part of the regex.

注意g"最后的标志正在为您完成大部分工作,以获取除明确捕获的两个匹配项之外的许多匹配项.

Note that the "g" flag at the end is doing most of that work for you in getting however many matches there are beyond the two explicitly captured.

我将捕获组保留为 ([a-z]*),就像您拥有它们一样.您可能需要使用 ([A-Za-z0-9]*) 以获得更大的灵活性.

I left the capture groups as ([a-z]*) as you had them. You may want to use ([A-Za-z0-9]*) for more flexibility.

这篇关于片段正则表达式:匹配任意数量的组并转换为 CamelCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆