Escaping characters before converting a wildcard pattern to a regular expression

94 views Asked by At

The user provides a search string, and this string is dynamically converted to a regex, because the subsequent search code doesn't take a plain string but a regex as input.

I want the characters * and ? to have special meanings in this user-provided string.

The current solution is to use wildcardToRegularExpression() (Qt 5 source code, Qt 6 source code):

QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion);

The above solution works great, but it has a flaw: the [ and ] characters also get special meanings, which I would like to avoid.

Thus, I tried to pattern-escape the [ and ] before feeding the string to wildcardToRegularExpression() (qBittorrent PR 16965, code):

// replace [ and ] with [[] and []], respectively
QString escapedPattern = pattern;
escapedPattern.replace(QRegularExpression(u"\\[|\\]"_qs), u"[\\0]"_qs);

return QRegularExpression::wildcardToRegularExpression(escapedPattern, QRegularExpression::UnanchoredWildcardConversion);

Sadly, this code wasn't working as expected, the search results were completely messed up, so it had to be reverted (qBittorrent PR 17820).

A while ago I had tried to investigate this, but just setting up the development environment had been a nightmare, and after many hours of headache I didn't succeed to complete it :\

Help would be greatly appreciated to understand and fix the issue. Here I'm aiming at Qt6, but notice there is also a Qt5 code path which had been encountering the same issue.

0

There are 0 answers