返回介绍

QRegExpValidator Class

发布于 2019-10-04 15:02:14 字数 5018 浏览 1321 评论 0 收藏 0

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

#include <qvalidator.h>

Inherits QValidator.

List of all member functions.

Public Members

  • QRegExpValidator ( QObject*parent, constchar*name = 0 )
  • QRegExpValidator ( constQRegExp&rx, QObject*parent, constchar*name = 0 )
  • ~QRegExpValidator ()
  • virtual QValidator::State validate ( QString&input, int&pos ) const
  • void setRegExp ( constQRegExp&rx )
  • const QRegExp & regExp () const

Detailed Description

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

QRegExpValidator contains a regular expression, "regexp", used to determine whether an input string is Acceptable, Intermediate or Invalid.

The regexp is treated as if it begins with the start of string assertion, ^, and ends with the end of string assertion $ so the match is against the entire input string, or from the given position if a start position greater than zero is given.

For a brief introduction to Qt's regexp engine see QRegExp.

Example of use:

    // regexp: optional '-' followed by between 1 and 3 digits
    QRegExp rx( "-?\\d{1,3}" );
    QRegExpValidator validator( rx, 0 );

    QLineEdit *edit = new QLineEdit( split );
    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
    QRegExp rx( "[1-9]\\d{0,3}" );
    // the validator treats the regexp as "^[1-9]\\d{0,3}$"
    QRegExpValidator v( rx, 0 );
    QString s;

    s = "0";     v.validate( s, 0 );    // returns Invalid
    s = "12345"; v.validate( s, 0 );    // returns Invalid
    s = "1";     v.validate( s, 0 );    // returns Acceptable

    rx.setPattern( "\\S+" );            // one or more non-whitespace characters
    v.setRegExp( rx );
    s = "myfile.txt";  v.validate( s, 0 ); // Returns Acceptable
    s = "my file.txt"; v.validate( s, 0 ); // Returns Invalid

    // A, B or C followed by exactly five digits followed by W, X, Y or Z
    rx.setPattern( "[A-C]\\d{5}[W-Z]" );
    v.setRegExp( rx );
    s = "a12345Z"; v.validate( s, 0 );  // Returns Invalid
    s = "A12345Z"; v.validate( s, 0 );  // Returns Acceptable
    s = "B12";     v.validate( s, 0 );  // Returns Intermediate

    // match most 'readme' files
    rx.setPattern( "read\\S?me(\.(txt|asc|1st))?" );
    rx.setCaseSensitive( FALSE );
    v.setRegExp( rx );
    s = "readme";      v.validate( s, 0 ); // Returns Acceptable
    s = "README.1ST";  v.validate( s, 0 ); // Returns Acceptable
    s = "read me.txt"; v.validate( s, 0 ); // Returns Invalid
    s = "readm";       v.validate( s, 0 ); // Returns Intermediate
  

See also QRegExp, QIntValidator, QDoubleValidator and Miscellaneous Classes.


Member Function Documentation

QRegExpValidator::QRegExpValidator ( QObject*parent, constchar*name = 0 )

Constructs a validator that accepts any string (including an empty one) as valid. The object's parent is parent and its name is name.

QRegExpValidator::QRegExpValidator ( constQRegExp&rx, QObject*parent, constchar*name = 0 )

Constructs a validator which accepts all strings that match the regular expression rx. The object's parent is parent and its name is name.

The match is made against the entire string, e.g. if the regexp is [A-Fa-f0-9]+ it will be treated as ^[A-Fa-f0-9]+$.

QRegExpValidator::~QRegExpValidator ()

Destroys the validator, freeing any resources allocated.

constQRegExp& QRegExpValidator::regExp () const

Returns the regular expression used for validation.

See also setRegExp().

void QRegExpValidator::setRegExp ( constQRegExp&rx )

Sets the regular expression used for validation to rx.

See also regExp().

QValidator::State QRegExpValidator::validate ( QString&input, int&pos ) const [virtual]

Returns Acceptable if input is matched by the regular expression for this validator, Intermediate if it has matched partially (i.e. could be a valid match if additional valid characters are added), and Invalid if input is not matched.

The start position is the beginning of the string unless pos is given and is > 0 in which case the regexp is matched from pos until the end of the string.

For example, if the regular expression is \w\d\d (that is, word-character, digit, digit) then "A57" is Acceptable, "E5" is Intermediate and "+9" is Invalid.

See also QRegExp::match().

Reimplemented from QValidator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文