I have a regular expression that worked with QRegExp, but is considered invalid by QRegularExpression:
\[[a-zA-Z0-9/^-*]+\]
In most regular expression systems I've come across, the asterisk has no special meaning inside a character class, but here apparently it's still not allowed?
What's worse, the backslash loses its role as an escape character, so this is still invalid:
\[[a-zA-Z0-9/^-\*]+\]
(note: for clarity I'm ignoring \ etc here)
I can get the desired result with QRegularExpression by writing:
\[([a-zA-Z0-9/^-]|\*)+\]
But still wondering: Why can't I use an asterisk inside []
in a QRegularExpression?
#!/usr/bin/env python3
from PySide6 import QtCore
r = QtCore.QRegularExpression(r'\[[a-zA-Z0-9/^-*]+\]')
print(r.isValid())
r = QtCore.QRegularExpression(r'\[[a-zA-Z0-9/^-\*]+\]')
print(r.isValid())
r = QtCore.QRegularExpression(r'\[([a-zA-Z0-9/^-]|\*)+\]')
print(r.isValid())
produces
False
False
True
Update: @G.M. figured it out: * is fine, but ^ and - are the problem:
#!/usr/bin/env python3
from PySide6 import QtCore
good = '[A*m^-2]'
bad = '[2 + 7]'
# My original regex
r = QtCore.QRegularExpression(r'\[[a-zA-Z0-9/^-*]+\]')
print('Valid' if r.isValid() else 'Not valid')
print(r.match(good).hasMatch())
print(r.match(bad).hasMatch())
print()
# Move ^ and - to the end of the class
r = QtCore.QRegularExpression(r'\[[a-zA-Z0-9/*^-]+\]')
print('Valid' if r.isValid() else 'Not valid')
print(r.match(good).hasMatch())
print(r.match(bad).hasMatch())
print()
# Or escape them
r = QtCore.QRegularExpression(r'\[[a-zA-Z0-9/\^\-*]+\]')
print('Valid' if r.isValid() else 'Not valid')
print(r.match(good).hasMatch())
print(r.match(bad).hasMatch())
produces
Not valid
QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object (pattern is '\[[a-zA-Z0-9/^-*]+\]')
False
QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object (pattern is '\[[a-zA-Z0-9/^-*]+\]')
False
Valid
True
False
Valid
True
False