请解释一下这个正则表达式

发布于 2024-08-14 18:42:20 字数 633 浏览 10 评论 0原文

正则表达式对我来说完全是空白。 我现在正在 TextMate 中处理一个可以完成我想要它做的事情...但我不知道为什么它会做我想要它做的事情。

/[[:alpha:]]+|( )/(?1::$0)/g

这在 TextMate 片段中使用,它的作用是获取一个 Label 并将其输出为 id 名称。因此,如果我在第一个位置输入“First Name”,则会输出“FirstName”。 以前它看起来像这样:

/[[:alpha:]]+|( )/(?1:_:/L$0)/g (it might have been \L instead)

这会将“First Name”变成“first_name”。 所以我知道下划线为空格添加了下划线,而 /L 将所有内容都小写了......但我无法弄清楚其余部分的作用或原因。

有人愿意逐条解释一下吗?

编辑

这是有问题的实际代码片段:

<column header="$1"><xmod:field name="${2:${1/[[:alpha:]]+|( )/(?1::$0)/g}}"/></column>

Regular Expressions are a complete void for me.
I'm dealing with one right now in TextMate that does what I want it to do...but I don't know WHY it does what I want it to do.

/[[:alpha:]]+|( )/(?1::$0)/g

This is used in a TextMate snippet and what it does is takes a Label and outputs it as an id name. So if I type "First Name" in the first spot, this outputs "FirstName".
Previously it looked like this:

/[[:alpha:]]+|( )/(?1:_:/L$0)/g (it might have been \L instead)

This would turn "First Name" into "first_name".
So I get that the underscore adds an underscore for a space, and that the /L lowercases everything...but I can't figure out what the rest of it does or why.

Someone care to explain it piece by piece?

EDIT

Here is the actual snippet in question:

<column header="$1"><xmod:field name="${2:${1/[[:alpha:]]+|( )/(?1::$0)/g}}"/></column>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌逼全场 2024-08-21 18:42:21
/[[:alpha:]]+|( )/(?1::$0)/g

(?1 是一个条件条件,如果组 1(单个空格)匹配,则用于删除匹配;如果组 1 不匹配,则用 $0 替换匹配由于 $0 是整个匹配项,因此在这种情况下它会被替换为自身:

/ //g

即删除所有空格

/[[:alpha:]]+|( )/(?1:_:/\L$0)/g

该正则表达式仍然使用相同的条件,但现在 if group 除外。 1 匹配,则将其替换为下划线,否则使用完整匹配 ($0),并由 \L 更改进行修改。它后面的所有文本都是大小写,因此 \LABC 会导致 abc;将其视为特殊控制代码。

/[[:alpha:]]+|( )/(?1::$0)/g

The (?1 is a conditional and used to strip the match if group 1 (a single space) was matched, or replace the match with $0 if group 1 wasn't matched. As $0 is the entire match, it gets replaced with itself in that case. This regex is the same as:

/ //g

I.e. remove all spaces.

/[[:alpha:]]+|( )/(?1:_:/\L$0)/g

This regex is still using the same condition, except now if group 1 was matched, it's replaced with an underscore, and otherwise the full match ($0) is used, modified by \L. \L changes the case of all text that comes after it, so \LABC would result in abc; think of it as a special control code.

静若繁花 2024-08-21 18:42:20

这种正则表达式 (regex) 格式基本上是:

 /matchthis/replacewiththis/settings

末尾的“g”设置意味着进行全局替换,而不仅仅是将正则表达式限制为特定行或选择。

进一步分解...

  [[:alpha:]]+|( )

它匹配一个字母数字字符(保存在参数 $0 中),或者可选的空格(保存在匹配参数 $1 中)。

  (?1::$0)

正如 Roger 所说,? 表示这部分是条件语句。如果在参数 $1 中找到匹配项,则将其替换为冒号 :: 之间的内容 - 在本例中什么也没有。如果$1 中没有任何内容,则匹配项将替换为$0 的内容,即任何不是空格的字母数字字符都将原样输出。

这解释了为什么第一个示例中的空格被删除,而第二个示例中的空格被下划线替换。

在第二个表达式中,\L 用于小写文本。

评论中的额外问题是如何在 TextMate 之外运行此表达式。以 vi 为例,我会将其分为多个步骤:

:0,$s/ //g
:0,$s/\u/\L\0/g

上述命令的第一部分告诉 vi 从 0< 行开始运行一个 substitution /code> 并在文件末尾结束(这就是 $ 的含义)。

表达式的其余部分使用与上面解释的相同类型的规则,尽管 vi 中的一些表示法有点自定义 - 请参阅此参考网页

This regular expression (regex) format is basically:

 /matchthis/replacewiththis/settings

The "g" setting at the end means do a global replace, rather than just restricting the regex to a particular line or selection.

Breaking it down further...

  [[:alpha:]]+|( )

That matches an alpha numeric character (held in parameter $0), or optionally a space (held in matching parameter $1).

  (?1::$0)

As Roger says, the ? indicates this part is a conditional. If a match was found in parameter $1 then it is replaced with the stuff between the colons :: - in this case nothing. If nothing is in $1 then the match is replaced with the contents of $0, i.e. any alphanumeric character that is not a space is output unchanged.

This explains why the spaces are removed in the first example, and the spaces get replaced with underscores in your second example.

In the second expression the \L is used to lowercase the text.

The extra question in the comment was how to run this expression outside of TextMate. Using vi as an example, I would break it into multiple steps:

:0,$s/ //g
:0,$s/\u/\L\0/g

The first part of the above commands tells vi to run a substitution starting on line 0 and ending at the end of the file (that's what $ means).

The rest of the expression uses the same sorts of rules as explained above, although some of the notation in vi is a bit custom - see this reference webpage.

因为看清所以看轻 2024-08-21 18:42:20

我发现 RegexBuddy 对我来说是处理正则表达式的好工具。我将您的第一个正则表达式粘贴到 Buddy 中,并得到了底部框架中显示的解释:

RegexBuddy

我用它来帮助理解现有的正则表达式,构建我自己的正则表达式,针对字符串测试正则表达式等。因此,我变得更好@正则表达式。仅供参考,我在 Ubuntu 上的 Wine 下运行。

I find RegexBuddy a good tool for me in dealing with regexs. I pasted your 1st regex in to Buddy and I got the explanation shown in the bottom frame:

RegexBuddy

I use it for helping to understand existing regexs, building my own, testing regexs against strings, etc. I've become better @ regexs because of it. FYI I'm running under Wine on Ubuntu.

芸娘子的小脾气 2024-08-21 18:42:20

它正在搜索连续至少出现一次的任何字母字符 [[:alpha:]]+ 或空格 ( )

it's searching for any alpha character that appears at least once in a row [[:alpha:]]+ or space ( ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文