文字分隔符( \Q \E 块内的分隔符) [英] Literal delimiter ( delimiter inside \Q \E block )

查看:80
本文介绍了文字分隔符( \Q \E 块内的分隔符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作一些基于 RegEx 的函数,其中大部分使用 \Q\E 作为一些RegEx pattern 是用户输入.

I've been trying to make a few of functions based on RegEx and most of them use \Q and \E as some of the RegEx pattern is user input.

所以,假设我们正在使用 delimiter / 并希望将它与 / 匹配,该函数将在其中构造一些东西/\Q/\E/ 的行.

So, let's say hypothetically that we're using the delimiter / and want to match it against / the function would construct something amongst the lines of /\Q/\E/.

我不知道为什么 /\Q/\E// 不匹配,但与其他所有分隔符匹配,除非您使用相同的分隔符输入.

I'm not sure why /\Q/\E/ doesn't match / but with every other delimiter it does, unless you use the same delimiter as input.

也许,它认为分隔符是结尾,即使它在一个纯文字块中并且转义符作为文字.不确定,试了一堆.

Maybe, it considers the delimiter the end, even though, it's in a literal-only block and the escape as literal. Not sure, tried a bunch.

希望有人能将我推向正确的方向,了解针对此问题的解决方法.

Hopefully someone can push me into the right direction as to what workarounds there are for this issue.

推荐答案

它有助于理解 / 不是正则表达式元字符,如 *(.它很特别,因为您使用它来分隔正则表达式本身,而转义正则表达式分隔符的唯一方法是使用反斜杠 (\/).

It helps to understand that / is not a regex metacharacter, like * or (. It's special because you're using it to delimit the regex itself, and the only way to escape the regex delimiter is with a backslash (\/).

但是您不需要使用 \Q\E.preg_quote() 方法需要一个delimiter 参数,因此它会在需要的地方正确添加反斜杠.

But you shouldn't need to use \Q and \E. The preg_quote() method takes a delimiter argument, so it correctly adds backslashes everywhere they're needed.

这篇关于文字分隔符( \Q \E 块内的分隔符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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