class QRegularExpressionValidator#

The QRegularExpressionValidator class is used to check a string against a regular expression. More

Inheritance diagram of PySide6.QtGui.QRegularExpressionValidator

Synopsis#

Properties#

Methods#

Slots#

Signals#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

QRegularExpressionValidator uses a regular expression (regexp) to determine whether an input string is Acceptable , Intermediate , or Invalid . The regexp can either be supplied when the QRegularExpressionValidator is constructed, or at a later time.

If the regexp partially matches against the string, the result is considered Intermediate . For example, “” and “A” are Intermediate for the regexp [A-Z][0-9] (whereas “_” would be Invalid ).

QRegularExpressionValidator automatically wraps the regular expression in the \\A and \\z anchors; in other words, it always attempts to do an exact match.

Example of use:

# regexp: optional '-' followed by between 1 and 3 digits
def rx("-?\\d{1,3}"):
validator = QRegularExpressionValidator(rx, self)
edit = QLineEdit(self)
edit.setValidator(validator)

Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above.

# integers 1 to 9999
def re("[1-9]\\d{0,3}"):
# the validator treats the regexp as "^[1-9]\\d{0,3}$"
v = QRegularExpressionValidator(re, 0)
s = QString()
pos = 0
s = "0" v.validate(s, pos); # returns Invalid
s = "12345" v.validate(s, pos); # returns Invalid
s = "1" v.validate(s, pos); # returns Acceptable
re.setPattern("\\S+") # one or more non-whitespace characters
v.setRegularExpression(re)
s = "myfile.txt" v.validate(s, pos); # Returns Acceptable
s = "my file.txt" v.validate(s, pos); # Returns Invalid
# A, B or C followed by exactly five digits followed by W, X, Y or Z
re.setPattern("[A-C]\\d{5}[W-Z]")
v.setRegularExpression(re)
s = "a12345Z" v.validate(s, pos); # Returns Invalid
s = "A12345Z" v.validate(s, pos); # Returns Acceptable
s = "B12" v.validate(s, pos); # Returns Intermediate
# match most 'readme' files
re.setPattern("read\\S?me(\\.(txt|asc|1st))?")
re.setPatternOptions(QRegularExpression.CaseInsensitiveOption)
v.setRegularExpression(re)
s = "readme" v.validate(s, pos); # Returns Acceptable
s = "README.1ST" v.validate(s, pos); # Returns Acceptable
s = "read me.txt" v.validate(s, pos); # Returns Invalid
s = "readm" v.validate(s, pos); # Returns Intermediate

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property regularExpressionᅟ: QRegularExpression#

This property holds the regular expression used for validation.

By default, this property contains a regular expression with an empty pattern (which therefore matches any string).

Access functions:
__init__([parent=None])#
Parameters:

parentQObject

Constructs a validator with a parent object that accepts any string (including an empty one) as valid.

__init__(re[, parent=None])
Parameters:

Constructs a validator with a parent object that accepts all strings that match the regular expression re.

regularExpression()#
Return type:

QRegularExpression

Getter of property regularExpressionᅟ .

regularExpressionChanged(re)#
Parameters:

reQRegularExpression

Notification signal of property regularExpressionᅟ .

setRegularExpression(re)#
Parameters:

reQRegularExpression

Setter of property regularExpressionᅟ .