「Qtの基礎 - 正規表現」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「== 概要 == <br><br> == QRegularExpressionクラス (推奨) == <br><br> == QRegExpクラス (非推奨) == <br><br> == エラー == Qt 6において、<code>QRegExp</code>クラスを使用する場合、以下に示すエラーが発生する。<br> QRegExp was not declared in this scope <br> このエラーは、Qt 6では<code>QRegExp</code>クラスが非推奨となり、<code>QRegularExpression</code>クラスを使用する必要がある。<br> <br> <…」) |
|||
6行目: | 6行目: | ||
== QRegExpクラス (非推奨) == | == QRegExpクラス (非推奨) == | ||
QRegExpクラスには技術的な限界があり、<br> | |||
PCRE (Perl Compatible Regular Expressions) との完全な互換性がなく、現代的な正規表現機能の一部をサポートしていない。<br> | |||
例えば、先読み・後読みのような高度なパターンマッチング機能が制限されている。<br> | |||
<br> | |||
==== 基本的なマッチング ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx1("hello"); | |||
bool match = rx1.exactMatch("hello"); // true | |||
qDebug() << "Basic match:" << match; | |||
</syntaxhighlight> | |||
<br> | |||
==== メタキャラクターを使用したパターン ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx2("\\d{3}-\\d{4}"); // 郵便番号パターン | |||
qDebug() << "Postal code match:" << rx2.exactMatch("123-4567"); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== 選択パターン ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx3("cat|dog"); | |||
qDebug() << "Alternative match:" << rx3.exactMatch("cat"); // true | |||
qDebug() << "Alternative match:" << rx3.exactMatch("dog"); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== グループ化とキャプチャ ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx4("(\\w+)@(\\w+\\.\\w+)"); | |||
QString text = "contact@example.com"; | |||
if (rx4.indexIn(text) != -1) { | |||
QString username = rx4.cap(1); // "contact" | |||
QString domain = rx4.cap(2); // "example.com" | |||
qDebug() << "Username:" << username << "Domain:" << domain; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 量指定子の使用 ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx5("a{2,4}"); // aが2回から4回 | |||
qDebug() << "Quantifier match:" << rx5.exactMatch("aaa"); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== 文字クラス ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx6("[A-Za-z0-9]+"); // 英数字の1回以上の繰り返し | |||
qDebug() << "Character class match:" << rx6.exactMatch("Test123"); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== テキスト置換 ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QString text2 = "The color is gray."; | |||
QRegExp rx7("gray"); | |||
text2.replace(rx7, "blue"); | |||
qDebug() << "Replaced text:" << text2; // "The color is blue." | |||
</syntaxhighlight> | |||
<br> | |||
==== 否定文字クラス ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx8("[^0-9]+"); // 数字以外の文字の1回以上の繰り返し | |||
qDebug() << "Negated class match:" << rx8.exactMatch("ABC"); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== 位置指定 ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx9("^Start"); // 行頭にStartがあるかチェック | |||
qDebug() << "Position match:" << rx9.exactMatch("Start of line"); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== グリーディマッチとノングリーディマッチ ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QString text3 = "<p>First</p><p>Second</p>"; | |||
QRegExp rx10("<p>.*</p>"); // グリーディマッチ | |||
QRegExp rx11("<p>.*?</p>"); // ノングリーディマッチ | |||
if (rx10.indexIn(text3) != -1) { | |||
qDebug() << "Greedy match:" << rx10.cap(0); // "<p>First</p><p>Second</p>" | |||
} | |||
if (rx11.indexIn(text3) != -1) { | |||
qDebug() << "Non-greedy match:" << rx11.cap(0); // "<p>First</p>" | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== ワイルドカードモード ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegExp> | |||
#include <QDebug> | |||
QRegExp rx12("doc*.txt", Qt::CaseSensitive, QRegExp::Wildcard); | |||
qDebug() << "Wildcard match:" << rx12.exactMatch("document.txt"); // true | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||
2024年10月25日 (金) 00:40時点における版
概要
QRegularExpressionクラス (推奨)
QRegExpクラス (非推奨)
QRegExpクラスには技術的な限界があり、
PCRE (Perl Compatible Regular Expressions) との完全な互換性がなく、現代的な正規表現機能の一部をサポートしていない。
例えば、先読み・後読みのような高度なパターンマッチング機能が制限されている。
基本的なマッチング
#include <QRegExp>
#include <QDebug>
QRegExp rx1("hello");
bool match = rx1.exactMatch("hello"); // true
qDebug() << "Basic match:" << match;
メタキャラクターを使用したパターン
#include <QRegExp>
#include <QDebug>
QRegExp rx2("\\d{3}-\\d{4}"); // 郵便番号パターン
qDebug() << "Postal code match:" << rx2.exactMatch("123-4567"); // true
選択パターン
#include <QRegExp>
#include <QDebug>
QRegExp rx3("cat|dog");
qDebug() << "Alternative match:" << rx3.exactMatch("cat"); // true
qDebug() << "Alternative match:" << rx3.exactMatch("dog"); // true
グループ化とキャプチャ
#include <QRegExp>
#include <QDebug>
QRegExp rx4("(\\w+)@(\\w+\\.\\w+)");
QString text = "contact@example.com";
if (rx4.indexIn(text) != -1) {
QString username = rx4.cap(1); // "contact"
QString domain = rx4.cap(2); // "example.com"
qDebug() << "Username:" << username << "Domain:" << domain;
}
量指定子の使用
#include <QRegExp>
#include <QDebug>
QRegExp rx5("a{2,4}"); // aが2回から4回
qDebug() << "Quantifier match:" << rx5.exactMatch("aaa"); // true
文字クラス
#include <QRegExp>
#include <QDebug>
QRegExp rx6("[A-Za-z0-9]+"); // 英数字の1回以上の繰り返し
qDebug() << "Character class match:" << rx6.exactMatch("Test123"); // true
テキスト置換
#include <QRegExp>
#include <QDebug>
QString text2 = "The color is gray.";
QRegExp rx7("gray");
text2.replace(rx7, "blue");
qDebug() << "Replaced text:" << text2; // "The color is blue."
否定文字クラス
#include <QRegExp>
#include <QDebug>
QRegExp rx8("[^0-9]+"); // 数字以外の文字の1回以上の繰り返し
qDebug() << "Negated class match:" << rx8.exactMatch("ABC"); // true
位置指定
#include <QRegExp>
#include <QDebug>
QRegExp rx9("^Start"); // 行頭にStartがあるかチェック
qDebug() << "Position match:" << rx9.exactMatch("Start of line"); // true
グリーディマッチとノングリーディマッチ
#include <QRegExp>
#include <QDebug>
QString text3 = "<p>First</p><p>Second</p>";
QRegExp rx10("<p>.*</p>"); // グリーディマッチ
QRegExp rx11("<p>.*?</p>"); // ノングリーディマッチ
if (rx10.indexIn(text3) != -1) {
qDebug() << "Greedy match:" << rx10.cap(0); // "<p>First</p><p>Second</p>"
}
if (rx11.indexIn(text3) != -1) {
qDebug() << "Non-greedy match:" << rx11.cap(0); // "<p>First</p>"
}
ワイルドカードモード
#include <QRegExp>
#include <QDebug>
QRegExp rx12("doc*.txt", Qt::CaseSensitive, QRegExp::Wildcard);
qDebug() << "Wildcard match:" << rx12.exactMatch("document.txt"); // true
エラー
Qt 6において、QRegExp
クラスを使用する場合、以下に示すエラーが発生する。
QRegExp was not declared in this scope
このエラーは、Qt 6ではQRegExp
クラスが非推奨となり、QRegularExpression
クラスを使用する必要がある。
#include <QRegularExpression>
static QRegularExpression RegEx("[+-]");
QString coordinate = "111-222+333";
QStringList parts = coordinate.split(RegEx, Qt::SkipEmptyParts);
QRegularExpression
クラスは、より強力で効率的な正規表現エンジンを提供しており、QRegExp
クラスと比較していくつかの利点がある。