QRegularExpression::wildcardToRegularExpression() not working?
-
I have a problem with wildcard expressions since migrating from QRegExp to QRegularExpression.
QRegularExpression::wildcardToRegularExpression("*")
returns this expression, is it correct?\A(?:[^/\\]*)\z
I expect that this expression should match any string, since that's the meaning of the
*
wildcard/glob expression, butQRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("abc").hasMatch()
returns false. This is the same way I was matching wildcard expressions with QRegExp (except it didn't need the wildcardToRegularExpression() conversion). Is the expression broken, or is there a problem with my match code? -
You're right , good point, I oversimplified the example. That works for me as well. This doesn't work:
const bool m = QRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("E:/Development/Projects/").hasMatch();
Qt 5.15.2, the latest available Qt 5. Windows, msvc_2019.
-
You're right , good point, I oversimplified the example. That works for me as well. This doesn't work:
const bool m = QRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("E:/Development/Projects/").hasMatch();
Qt 5.15.2, the latest available Qt 5. Windows, msvc_2019.
@Violet-Giraffe
Because the point of the regular expression is to fail against any/
or\
s. You should have shown what you were testing it against.any string, since that's the meaning of the * wildcard/glob expression
Wildcard/globbing should not match directory separators.
Discussed in detail at https://doc.qt.io/qt-5/qregularexpression.html#wildcardToRegularExpression, https://doc.qt.io/qt-5/qregularexpression.html#wildcard-matching
-
@Violet-Giraffe
Because the point of the regular expression is to fail against any/
or\
s. You should have shown what you were testing it against.any string, since that's the meaning of the * wildcard/glob expression
Wildcard/globbing should not match directory separators.
Discussed in detail at https://doc.qt.io/qt-5/qregularexpression.html#wildcardToRegularExpression, https://doc.qt.io/qt-5/qregularexpression.html#wildcard-matching
@JonB said in QRegularExpression::wildcardToRegularExpression() not working?:
Because the point of the regular expression is to fail against any / or \s.
That's absolutely stupid for a glob expression. Is there a way to fix this while still using
wildcardToRegularExpression
? Or is the only way to manually edit the returned expression, e. g. replace[^/\]*
with.*
?I have read most of the https://doc.qt.io/qt-5/qregularexpression.html page before asking here, nothing related to my problem is discussed there, let alone - in detail.
-
Hi,
Glob patterns work at the file name level not on full paths. As stated in the method documentation, the current implementation follows closely the definition listed there. You can go through the patch review of that method for more details.
Out of curiosity, what is your goal using
.*
since you will match anything with that ? -
Thank you for the clarification and explanation. I did see the remark that the implementation now follows those rules, and didn't check the rules themselves, my bad. It worked just as expected with QRegExp, so I hoped it would work with QRegularExpression the same way if I migrate the code correctly.
Of course, the
*
glob is a corner case, usually it's a combination of text characters and wildcards. But it's important for me to support full paths. Now it's clear I need to change my surrounding code, once again - thank you for the explanation!