matching multiple patterns wildcard and regexp
-
the line edit input in my application allows to enter string to search among items by matching the string by wildcard or regexp.
i need to support input of multiple patterns such as
abc* *def
and filter items based on any of them. i tried to split the input by space, then concatenate them by|
to allow matching withOR
operator. this would allow to iterate over the items once.
so i have(abc*|*def)
but when i set this pattern toQRegExp
class, it doesn't correctly match the items.so current solution for me is to split the input by space, then for each pattern iterate over all items and filter items based on each pattern. this means iterating over the set as many times as the patterns.
what can i do to optimize the solution?
-
@user4592357 said in matching multiple patterns wildcard and regexp:
so i have
(abc*|*def)
but when i set this pattern toQRegExp
class, it doesn't correctly match the items.Should you be using
QRegularExpression
? What version of Qt? -
Hi,
QRegExp is deprecated in Qt 5 and removed in Qt 6 so go with QRegularExpression.
To build proper wildcard regular expression, take a look at QRegularExpression::wildcardToRegularExpression.
That said, can you show a real input example and group of expression you want to use ? And what result you would expect to have ?
On a side note, there's a helper tool you can build and run to test your regular expressions.
-
I don't know your knowledge level on the regexp subject but the output you expect won't be returned by the expression you suggest.
At best you would have everything including and after abcert since your expression more or less says: abc followed by anything or nothing, followed by a space followed by def followed by anything or nothing.
-
@SGaist i would have to have a different logic that the normal one.
i have to split this input pattern string by space to retrieve separate patterns (space splitting is safe since my string set can't contain space). then i need to match each of my strings to each pattern. this has complexity O(n*m), where n is the number of strings in set, and m patterns. so i was wondering if it can be made O(n). -
@user4592357 said in matching multiple patterns wildcard and regexp:
this has complexity O(n*m), n string set, m patterns. so i was wondering if it can be made O(n).
:)
BTW, do you have hundreds of patterns to match against hundred of "words"?
A regular expression like
\b(abc|def|ghi)\B*
might/should deliver multiple matches against your patterns. Of course you would have to build that pattern string from your desired strings. Whether it would be faster than doing your own stuff on each word in practice I couldn't say. You might think of it as O(n), but there's quite a bit going on inside the regular expression matching code to deal with the m patterns from the O(n * m) that you are passing in via the
(...|...)
in the string. No such thing as a free lunch ;-)