📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

ページの作成:「== 概要 == <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>