当正则表达式被@符号包围时,这是什么意思? [英] What does it mean when a regular expression is surrounded by @ symbols?
问题描述
问题
当正则表达式被@符号包围时,这是什么意思?这是否意味着与被斜杠包围不一样? @x或@i何时结束呢?现在,我想一想,周围的斜线甚至意味着什么?
What does it mean when a regular expression is surrounded by @ symbols? Does that mean something different than being surround by slashes? What about when @x or @i are on the end? Now that I think about it, what do the surrounding slashes even mean?
背景
我看到了John发表的 StackOverflow答案 Kugelman,在那里他表现出认真的Regex技能.
I saw this StackOverflow answer, posted by John Kugelman, in which he displays serious Regex skills.
现在,我已经习惯于看到正则表达式被斜线包围
Now, I'm used to seeing regexes surrounded by slashes as in
/^abc/
但是他使用了用@符号包围的正则表达式:
But he used a regex surrounded by @ symbols:
'@
^%
(.{2}) # State, 2 chars
([^^]{0,12}.) # City, 13 chars, delimited by ^
([^^]{0,34}.) # Name, 35 chars, delimited by ^
([^^]{0,28}.) # Address, 29 chars, delimited by ^
\?$
@x'
实际上,它似乎采用以下格式:
In fact, it seems to be in the format:
@^abc@x
在尝试搜索google的含义的过程中(这对google来说是个难题),我还看到了以下格式:
In the process of trying to google what that means (it's a tough question to google!), I also saw the format:
@^abc@i
很明显 x 和 i 是不匹配的字符.
It's clear the x and the i are not matched characters.
那是什么意思?
在此先感谢您的回复,
-gMale
推荐答案
周围的斜杠只是正则表达式分隔符.您可以使用任何字符(afaik)来执行此操作-最常用的是/,我见过的其他常用的是#
The surrounding slashes are just the regex delimiters. You can use any character (afaik) to do that - the most commonly used is the /, other I've seen somewhat commonly used is #
换句话说,@whatever@i
本质上与/whatever/i
相同(i是不区分大小写的匹配项的修饰符)
So in other words, @whatever@i
is essentially the same as /whatever/i
(i is modifier for a case-insensitive match)
您可能要使用/以外的其他原因是如果您的正则表达式包含字符.您避免逃脱它,类似于使用''代替字符串".
The reason you might want to use something else than the / is if your regex contains the character. You avoid having to escape it, similar to using '' for strings instead of "".
这篇关于当正则表达式被@符号包围时,这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!