如何“排除" RegExp分组中是否有空白? [英] How to "exclude" a blank space from RegExp grouping?

查看:90
本文介绍了如何“排除" RegExp分组中是否有空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从事ExtJS框架开发的前端开发人员,出于提高速度的目的,我创建了许多Sublime Text 3的代码片段以简化工作.

I am a front-end developer working on ExtJS framework, and for purpose of speed, I created a lot of Sublime Text 3's Snippets to agilize the work.

任务模型来自C#平台的后端,其中包含var的类型及其名称.

The models of a task, comes from the back-end at C# platform, containing the type of var and the name of it.

然后我有了一个想法,就是简单地复制模型内容,并使用其字符串生成一个新字符串,该字符串等同于ExtJS模型模式.

Then I got an idea to simply copy the model content and with its string produce a new string that equates to ExtJS model pattern.

在代码段代码中,我没有使用任何编程语言(因为不可能),我只使用正则表达式解决方案生成输出字符串,由于Sublime Text代码段的限制,这是我唯一能做的事情.

Inside the Snippet code, I am not using any programming language (because its not possible), I am only producing output string with regex-only solution, that is the only one thing I can do due to Sublime Text snippet limitations.

一行C#模型代码的示例是:

A sample of one line of C# model code is:

  public string Email { get; set; }

模型的所有行均遵循该模式.

All the lines of the model follow that pattern.

此刻,我的Sublime Text 3代码段具有以下代码:

At the moment, my Sublime Text 3 Snippet has the code:

<snippet>
    <content><![CDATA[
    { name: '${SELECTION/(        public )|(public )|({ get; set; })|(\w)|( \w+)|( )/(?5$5/\s/)/g}', type: '${SELECTION/(        public )|(public )|({ get; set; })|(\ \$w\ \w.)|( \w+)|( )/(?1)(?2)(?3)(?4\$5\$6\)($5)($6)/g}' },
]]></content>
    <tabTrigger>modelnames</tabTrigger>
</snippet>

PS:${SELECTION} var是一个片段var,它获取触发该片段时选择的字符串. 使用我的代码段并选择了示例字符串得到的字符串是:

PS: the ${SELECTION} var is a snippet var that gets the string that was selected when you triggered the snippet. The string that results using my snippet with that sample string selected is:

  { type: 'string', name: ' Email' },

问题

正如您在上面看到的,我得到了几乎完美的结果,但是我的问题是Email之前的空白.

我已经尝试了数百万种不同的组合,因此,我是regex的初学者,无法解决这个问题.

I have tried millions of different combinations and so, but I am beginner on regex and cant solve that.

我认为创建一个匹配空格的组并从主要组目标字符串中排除来解决问题,但是我不知道该怎么做,事实是正则表达式因为我是regex的初学者,所以我采用的是基于错误的方法.

I think that creating a group that match a space and excluding it from the main group target string solves the problem, but I don't know how to do that exactly, the truth is that regex that I've generated was on trail-and-error method, because I am a beginner at regex.

我要的是帮助删除该空间的方法,这对RegExp专家来说可能是一个简单的任务.

I'm asking a help to remove that space only, probably its a simple task to a RegExp expert.

推荐答案

您可以使用:

<snippet>
    <content><![CDATA[
    { ${SELECTION/\s*\bpublic\s+([\w<>]+)\s+(\w+).*/'name': '$2', 'type': '$1'/} },
]]></content>
    <tabTrigger>modelnames</tabTrigger>
</snippet>

将会输出

    { 'name': 'Email', 'type': 'string' },

对于您的示例

        public string Email { get; set; }

工作原理:

  • \s*\bpublic-匹配任意数量的空格字符,后跟单词边界,后跟public
  • \s+匹配至少一个空格字符
  • ([\w<>]+)匹配至少一个单词字符或尖括号(以在支持通用类型的情况下(如果有用)支持)并将结果存储到捕获组1
  • \s+匹配至少一个空格字符
  • (\w+)匹配至少一个单词字符(标识符)并将结果存储到捕获组2中
  • .*匹配其余选择
  • /开始更换
  • 替换为'name': '$2', 'type': '$1',其中捕获组1中将填充$1,捕获组2中将填充$2
  • /末端更换
  • \s*\bpublic - match any number of whitespace characters, followed by a word boundary, followed by public
  • \s+ match at least one whitespace character
  • ([\w<>]+) match at least one word character or angle bracket (to support generic types, in case it's useful) and store the result into capture group 1
  • \s+ match at least one whitespace character
  • (\w+) match at least one word character (the identifier) and store the result into capture group 2
  • .* match the rest of the selection
  • / begin replacement
  • replace with 'name': '$2', 'type': '$1', where $1 will be filled from capture group 1, and $2 from capture group 2
  • / end replacement

我没有包括g lobal标志,因为此正则表达式每行/每次选择仅需要匹配一次.

I didn't include the global flag because this regex only needs to match once per line/selection.

实际上,我们可以使其替换您选择的所有此类行:

Actually, we could make it replace all such lines in your selection:

<snippet>
    <content><![CDATA[
${SELECTION/\s*\bpublic\s+([\w<>]+)\s+(\w+).*?(\n|$)/    \{ 'name': '$2', 'type': '$1' \},\n/g}
]]></content>
    <tabTrigger>modelnames</tabTrigger>
</snippet>

会转换

        public string Email { get; set; }
        public string Name { get; set; }

    { 'name': 'Email', 'type': 'string' },
    { 'name': 'Name', 'type': 'string' },


基于有关映射类型的注释中的反馈,这是新的摘录内容:


based on feedback in the comments about mapping types, here is the new snippet content:

<snippet>
    <content><![CDATA[
${SELECTION/\s*\bpublic\s+(?:(DateTime)|(bool)|(decimal)|([\w<>]+))\s+(?<name>\w+).*?(\n|$)/    \{ name: '$+{name}', type: '(?1date)(?2boolean)(?3float)(?4$4)' \},\n/g}
]]></content>
    <tabTrigger>modelnames</tabTrigger>
</snippet>

字符串替换格式记录在

The string replacement format is documented at http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html.

在这里,为简单起见,我使用的是名为name的命名捕获组.

Here, I'm using a named capture group, called name, for simplicity.

这篇关于如何“排除" RegExp分组中是否有空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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