转义正则表达式中的正斜杠
我的问题很简单,是关于正则表达式转义的。是否必须在正则表达式中转义正斜杠 /
?你会怎样做呢?
My question is a simple one, and it is about regular expression escaping. Do you have to escape a forward slash /
in a regular expression? And how would you go about doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
什么上下文/语言?有些语言使用
/
作为模式分隔符,所以是的,您需要转义它,具体取决于哪种语言/上下文。您可以通过在其前面放置一个反斜杠来转义它:\/
对于某些语言(如 PHP),您可以使用其他字符作为分隔符,因此不需要对其进行转义。但据我所知,在所有语言中,/
唯一的特殊意义是它可能是指定的模式分隔符。What context/language? Some languages use
/
as the pattern delimiter, so yes, you need to escape it, depending on which language/context. You escape it by putting a backward slash in front of it:\/
For some languages (like PHP) you can use other characters as the delimiter and therefore you don't need to escape it. But AFAIK in all languages, the only special significance the/
has is it may be the designated pattern delimiter.以下是一些选项:
在 Perl 中,您可以选择备用分隔符。您并不局限于
m//
。您可以选择另一个,例如m{}
。那么逃跑就没有必要了。事实上,Damian Conway 在“Perl 最佳实践”中断言m{}
是唯一应该使用的替代分隔符,Perl::Critic(在 CPAN 上)强化了这一点。虽然您可以使用各种替代分隔符,但//
和{}
似乎是稍后最容易解读的。但是,如果这些选择中的任何一个导致过多的转义,请选择最适合易读的选项。常见示例有m(...)
、m[...]
和m!...!
。如果您不能或不愿意使用备用分隔符,您可以使用反斜杠转义正斜杠:例如
m/\/[^/]+$/
(使用备用分隔符可能会变成m{/[^/]+$}
,这样读起来可能会更清楚)。用反斜杠转义斜杠是很常见的,以至于赢得了一个名字和一个维基百科页面:倾斜牙签综合症。在只有一个实例的正则表达式中,转义斜杠可能不会上升到被视为妨碍可读性的程度,但如果它开始失控,并且如果您的语言像 Perl 一样允许使用替代分隔符,那就会是首选解决方案。Here are a few options:
In Perl, you can choose alternate delimiters. You're not confined to
m//
. You could choose another, such asm{}
. Then escaping isn't necessary. As a matter of fact, Damian Conway in "Perl Best Practices" asserts thatm{}
is the only alternate delimiter that ought to be used, and this is reinforced by Perl::Critic (on CPAN). While you can get away with using a variety of alternate delimiter characters,//
and{}
seem to be the clearest to decipher later on. However, if either of those choices result in too much escaping, choose whichever one lends itself best to legibility. Common examples arem(...)
,m[...]
, andm!...!
.In cases where you either cannot or prefer not to use alternate delimiters, you can escape the forward slashes with a backslash:
m/\/[^/]+$/
for example (using an alternate delimiter that could becomem{/[^/]+$}
, which may read more clearly). Escaping the slash with a backslash is common enough to have earned a name and a wikipedia page: Leaning Toothpick Syndrome. In regular expressions where there's just a single instance, escaping a slash might not rise to the level of being considered a hindrance to legibility, but if it starts to get out of hand, and if your language permits alternate delimiters as Perl does, that would be the preferred solution.使用反斜杠
\
或选择不同的分隔符,即m#.\d#
而不是/.\d/
“在 Perl 中,如果您在 / 正则表达式分隔符前面加上字母 m(用于匹配),则可以将其更改为几乎任何其他特殊字符;”
Use the backslash
\
or choose a different delimiter, iem#.\d#
instead of/.\d/
"In Perl, you can change the / regular expression delimiter to almost any other special character if you preceed it with the letter m (for match);"
如果分隔符是/,则需要转义。
If the delimiter is /, you will need to escape.
如果您使用 C#,则无需转义它。
If you are using C#, you do not need to escape it.
对于java,你不需要。
如果你把\放在/前面。 IDE 会告诉您“ReGex 中的冗余字符转义“\/””
For java, you don't need to.
If you put \ in front of /. IDE will tell you "Redundant Character Escape "\/" in ReGex"