このページでは

QRegularExpressionValidator Class

QRegularExpressionValidator クラスは、文字列が正規表現に一致するかどうかを検証するために使用されます。詳細...

ヘッダー: #include <QRegularExpressionValidator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
継承元: QValidator

プロパティ

パブリック関数

QRegularExpressionValidator(QObject *parent = nullptr)
QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr)
virtual ~QRegularExpressionValidator()
QRegularExpression regularExpression() const

再実装されたパブリック関数

virtual QValidator::State validate(QString &input, int &pos) const override

パブリックスロット

void setRegularExpression(const QRegularExpression &re)

シグナル

void regularExpressionChanged(const QRegularExpression &re)

詳細な説明

QRegularExpressionValidator は、正規表現 (regexp) を使用して、入力文字列がAcceptable 、Intermediate 、またはInvalid のいずれであるかを判定します。正規表現は、QRegularExpressionValidator のインスタンス化時に指定することも、後で指定することもできます。

正規表現が文字列と部分的に一致した場合、結果はIntermediate とみなされます。たとえば、正規表現[A-Z][0-9]に対して、"" や "A" はIntermediate に該当します(一方、"_" はInvalid に該当します)。

QRegularExpressionValidatorは、正規表現を自動的に\\A および\\z アンカーで囲みます。つまり、常に完全一致を試みます。

使用例:

// regexp: optional '-' followed by between 1 and 3 digits
QRegularExpression rx("-?\\d{1,3}");
QValidator *validator = new QRegularExpressionValidator(rx, this);

QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);

以下に、バリデータの一例をいくつか示します。実際には、上記の例のように、通常はウィジェットに関連付けられます。

// integers 1 to 9999
QRegularExpression re("[1-9]\\d{0,3}");
// the validator treats the regexp as "^[1-9]\\d{0,3}$"
QRegularExpressionValidator v(re, 0);
QString s;
int 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

QRegularExpression 、QIntValidator 、およびQDoubleValidatorも参照してください 。

プロパティのドキュメント

regularExpression : QRegularExpression

このプロパティには、検証に使用される正規表現が格納されます

デフォルトでは、このプロパティにはパターンが空の正規表現が含まれています(したがって、任意の文字列に一致します)。

アクセス関数:

QRegularExpression regularExpression() const
void setRegularExpression(const QRegularExpression &re)

通知シグナル:

void regularExpressionChanged(const QRegularExpression &re)

メンバ関数のドキュメント

[explicit] QRegularExpressionValidator::QRegularExpressionValidator(QObject *parent = nullptr)

parent オブジェクトを使用して、任意の文字列(空文字列を含む)を有効な値として受け入れるバリデータを作成します。

[explicit] QRegularExpressionValidator::QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr)

正規表現 `re` に一致するすべての文字列を受け入れる、parent オブジェクトを使用したバリデータを作成します。

[virtual noexcept] QRegularExpressionValidator::~QRegularExpressionValidator()

バリデータを取り消します。

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

QValidator::validate(QString &input, int &pos) const を再実装します。

input がこのバリデータ用の正規表現と一致した場合はAcceptable を、部分的に一致した場合(つまり、有効な文字が追加されれば有効な一致となる場合)はIntermediate を、input が一致しなかった場合はInvalid を返します。

input に一致しない場合、pos パラメータはinput パラメータの長さに設定されます。それ以外の場合は変更されません。

たとえば、正規表現が \w\d\d (単語文字、数字、数字) の場合、「A57」は `Acceptable` となり、「E5」は `Intermediate` となり、「+9」は `Invalid` となります。

QRegularExpression::match()も参照してください 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.