[英]Escape regex special characters for tr1::regex
I need to embed user-input in my regular expression , so it needs to be escaped for any regex special characters, and I don't know in advance what the string will be. 我需要在我的正则表达式中嵌入用户输入 ,因此需要为任何正则表达式特殊字符进行转义,而且事先我不知道该字符串是什么。
It would be something like 它会是这样的
string pattern = "\\d+ " + myEscapeFunction(userData);
Which special characters do I need to escape? 我需要逃脱哪些特殊角色? Or is there an equivalent function to Qt's QRegExp::escape ?
或者是否有与Qt的QRegExp :: escape相同的功能?
The list of characters that you have to escape depends on which of the various regular expression grammars you're using. 您必须转义的字符列表取决于您正在使用的各种正则表达式语法。 If you're using the default
ECMAScript
, it looks like the list in the QRegExp::escape
documentation is a good place to start. 如果你使用默认的
ECMAScript
,看起来QRegExp::escape
文档中的列表是一个很好的起点。 It says: 它说:
The special characters are $, (,), *, +, ., ?, [, ,], ^, {, |
特殊字符是$,(,),*,+,。,?,[,,],^,{,| and }.
和}。
That list leaves out \\
for some reason. 由于某种原因,该列表遗漏了
\\
。
But it's slightly more complicated than that, because inside square brackets, none of the characters except \\
and ]
are special, and \\]
has to stay unescaped. 但它稍微复杂一些,因为在方括号内,除了
\\
和]
之外的所有字符都不是特殊的,而\\]
必须保持未转义状态。
Further, a ?
还有,一个
?
that comes right after a (
is not special. For example, in (?=x)
the ?
should not be escaped. 自带之后立即
(
不是特殊的。例如,在(?=x)
的?
不应该逃脱。
I think that's pretty much it, but I haven't put enough time into this to be sure. 我认为这几乎就是它,但我还没有给它足够的时间来确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.