转义正则表达式中的正斜杠

发布于 2024-11-09 02:45:49 字数 67 浏览 2 评论 0原文

我的问题很简单,是关于正则表达式转义的。是否必须在正则表达式中转义正斜杠 / ?你会怎样做呢?

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 技术交流群。

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

发布评论

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

评论(6

不羁少年 2024-11-16 02:45:49

什么上下文/语言?有些语言使用 / 作为模式分隔符,所以是的,您需要转义它,具体取决于哪种语言/上下文。您可以通过在其前面放置一个反斜杠来转义它: \/ 对于某些语言(如 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.

初相遇 2024-11-16 02:45:49

以下是一些选项:

  • 在 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 as m{}. Then escaping isn't necessary. As a matter of fact, Damian Conway in "Perl Best Practices" asserts that m{} 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 are m(...), m[...], and m!...!.

  • 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 become m{/[^/]+$}, 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.

萌无敌 2024-11-16 02:45:49

使用反斜杠 \ 或选择不同的分隔符,即 m#.\d# 而不是 /.\d/
“在 Perl 中,如果您在 / 正则表达式分隔符前面加上字母 m(用于匹配),则可以将其更改为几乎任何其他特殊字符;”

Use the backslash \ or choose a different delimiter, ie m#.\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);"

So要识趣 2024-11-16 02:45:49

如果分隔符是/,则需要转义。

If the delimiter is /, you will need to escape.

尸血腥色 2024-11-16 02:45:49

如果您使用 C#,则无需转义它。

If you are using C#, you do not need to escape it.

憧憬巴黎街头的黎明 2024-11-16 02:45:49

对于java,你不需要。

eg: "^(.*)/\\*LOG:(\\d+)\\*/(.*)$" ==> ^(.*)/\*LOG:(\d+)\*/(.*)$

如果你把\放在/前面。 IDE 会告诉您“ReGex 中的冗余字符转义“\/””

For java, you don't need to.

eg: "^(.*)/\\*LOG:(\\d+)\\*/(.*)$" ==> ^(.*)/\*LOG:(\d+)\*/(.*)$

If you put \ in front of /. IDE will tell you "Redundant Character Escape "\/" in ReGex"

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