0

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.

Gras Double
  • 15,901
  • 8
  • 56
  • 54
  • looks like you want to use a glob? – ניר Aug 30 '23 at 06:14
  • It seems you want to disable the special meaning of `[...]` inside glob. In this regard, I doubt there is a way to do this with the function. I think you should 1) use `QRegularExpression::escape(glob_pattern)` and then `.replace("\\?", ".")` and then `.replace("\\*", ".*")`. – Wiktor Stribiżew Aug 30 '23 at 06:59
  • @WiktorStribiżew actually, looks like substituting `[` with `[[]` before calling should work (so perhaps the substitution in the question is being done incorrectly). note that the function treats `/` ( "\\" on windows) specially – jhnc Aug 30 '23 at 07:03

0 Answers0