Question

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?

Was it helpful?

Solution

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

I think that's pretty much it, but I haven't put enough time into this to be sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top