Literal delimiter ( delimiter inside \Q \E block )

扶醉桌前 提交于 2019-12-20 04:45:08

问题


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.

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/.

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.


回答1:


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 (\/).

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.



来源:https://stackoverflow.com/questions/41526755/literal-delimiter-delimiter-inside-q-e-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!