2

I would like to provide wildcard matching in a search box for users of my application. I have a QTableView filled with details of parts used in manufacture, obe column is the part number which is an alphanumeric field with some "-". Id like the user to be able to enter "123*" into the search box to have all the parts not starting with "123" to be hidden. Similarly with "*678", "*66*88*" etc. That is, multiple wildcard characters in the search string.

I was looking at QRegularExpression documentation and saw this in the section "Notes for QRegExp users":

There is no equivalent of wildcard matching in QRegularExpression. Nevertheless, rewriting a regular expression in wildcard syntax to a Perl-compatible regular expression is a very easy task, given the fact that wildcard syntax supported by QRegExp is very simple.

Can you please show me how I can "very easily" rewrite a "*88*99*" string to a regular expression?

I should say that some of my users are techie and will appreciate the ability to use regexp in the search field, but most aren't and I'd like to give them the easy wildcard option ('cos I'm a kind coder :) ).

Michael Vincent
  • 1,620
  • 1
  • 20
  • 46
  • Does it just mean the `.` that Perl uses? In PCRE (Perl compliant regexes, I think), `88*89` would match `8, followed by any number of 8s, followed by 89` since the `*` binds to the previous part of the expression. Match=`88888889`, no match=`88abc89`. To get what you want, in Perl, you would need `88.*89` since `.` means `any character`. Now both `88889` and `88abc89` should match. All that said, I haven't used `QRegExp` so I don't know if that's what they're talking about. – dwanderson Nov 13 '15 at 15:58
  • You just need to replace `*` with `.*` in Qt regex. `.` matches any character including a newline in Qt. – Wiktor Stribiżew Nov 13 '15 at 16:02
  • Thank you guys for your quick comments/answers. I'll give it a try. – Michael Vincent Nov 13 '15 at 16:33

2 Answers2

7

I would look into using QRegExp with setPatternSyntax(QRegExp::Wildcard).

http://doc.qt.io/qt-5/qregexp.html#PatternSyntax-enum

QRegExp::Wildcard 1 This provides a simple pattern matching syntax similar to that used by shells (command interpreters) for "file globbing". See Wildcard Matching.

http://doc.qt.io/qt-5/qregexp.html#wildcard-matching

c Any character represents itself apart from those mentioned below. Thus c matches the character c.

? Matches any single character. It is the same as . in full regexps.

* Matches zero or more of any characters. It is the same as .* in full regexps.

[...] Sets of characters can be represented in square brackets, similar to full regexps. Within the character class, like outside, backslash has no special meaning.

Asking the average end user to deal with regular expressions may be a lot. The file globbing supported by Windows search and other basic search functions is a lot easier to swallow for most users. So with QRegExp::Wildcard you are using a restricted subset of Regular Expressions, as described above.

And for a random side note: In SQL it uses the % as the wildcard character with the LIKE keyword.

If you want to play around in regular expressions in general, I recommend trying http://regexr.com/ .

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • 3
    Hasn't QRegExp been replaced or is being replaced by QRegularExpression ? – Michael Vincent Nov 17 '15 at 14:55
  • In many ways, yes, but it isn't deprecated yet. A number of classes in Qt only take a `QRegExp` and don't take the `QRegularExpression`... Here is the note at the tail end of QRegExp in the qt5 docs: `Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp.` http://doc.qt.io/qt-5/qregexp.html#details And here is another related link: http://stackoverflow.com/questions/29532836/is-there-any-difference-between-qregularexpression-and-qregexp – phyatt Nov 17 '15 at 16:39
  • Thank you for the clarification – Michael Vincent Nov 18 '15 at 11:25
  • QRegExp is marked deprecated in Qt6 – ManuelSchneid3r Dec 07 '22 at 09:31
0

This is old question, but I want to leave an example of code for those who porting code to Qt6. Since Qt 5.15 you can use static method QRegularExpression::wildcardToRegularExpression.

QString wildcardExp = QRegularExpression::wildcardToRegularExpression("*.jpeg");
QRegularExpression re(QRegularExpression::anchoredPattern(wildcardExp),
                      QRegularExpression::CaseInsensitiveOption);
re.match(file).hasMatch();
dismine
  • 575
  • 13
  • 17