「Qtの基礎 - 正規表現」の版間の差分
ナビゲーションに移動
検索に移動
(→エラー) |
|||
3行目: | 3行目: | ||
== QRegularExpressionクラス (推奨) == | == QRegularExpressionクラス (推奨) == | ||
QRegularExpressionクラスは、PCREライブラリを基盤としており、以下に示すようなメリットがある。<br> | |||
* パフォーマンスの向上 | |||
: 内部実装が最適化されており、特に複雑なパターンマッチングでより高速に動作する。 | |||
* エラーハンドリングの改善 | |||
*: マッチング結果やエラー状態をより詳細に把握できるようになった。 | |||
*: 以前のQRegExpクラスでは、エラーが発生した際の情報が限定的であった。 | |||
* Unicode対応の強化 | |||
*: 現代的なテキスト処理に必要な、より包括的なUnicodeサポートを提供している。 | |||
<br> | |||
QRegularExpressionクラスを使用することにより、コードの保守性が向上して、より堅牢なテキスト処理機能を実装できる。<br> | |||
移行を検討する場合は、既存のQRegExpクラスを使用したコードにおいて、新しい機能の実装時やコードのメンテナンス時に徐々にQRegularExpressionクラスへ移行することが推奨される。<br> | |||
<br> | |||
<br><br> | <br><br> | ||
2024年10月25日 (金) 00:42時点における版
概要
QRegularExpressionクラス (推奨)
QRegularExpressionクラスは、PCREライブラリを基盤としており、以下に示すようなメリットがある。
- パフォーマンスの向上
- 内部実装が最適化されており、特に複雑なパターンマッチングでより高速に動作する。
- エラーハンドリングの改善
- マッチング結果やエラー状態をより詳細に把握できるようになった。
- 以前のQRegExpクラスでは、エラーが発生した際の情報が限定的であった。
- Unicode対応の強化
- 現代的なテキスト処理に必要な、より包括的なUnicodeサポートを提供している。
QRegularExpressionクラスを使用することにより、コードの保守性が向上して、より堅牢なテキスト処理機能を実装できる。
移行を検討する場合は、既存のQRegExpクラスを使用したコードにおいて、新しい機能の実装時やコードのメンテナンス時に徐々に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
クラスと比較していくつかの利点がある。