📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の4版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
Qtでは、<code>QRegularExpression</code>クラスを使用して正規表現を扱う。<br> | |||
これは現代的な正規表現エンジンを提供しており、Perl互換の正規表現構文をサポートしている。<br> | |||
<br> | |||
<u>Qt 5.11以前まで使用されていたQRegExpクラスは現在非推奨となっている。</u><br> | |||
<br> | |||
基本的な使用方法としては、パターンマッチングとテキスト置換が主な用途である。<br> | |||
<br> | |||
パターンマッチングでは、文字列が特定のパターンに一致するかどうかを確認する。<br> | |||
例えば、Eメールアドレスの検証やURLの抽出等に活用できる。<br> | |||
<br> | |||
<code>QRegularExpression</code>クラスには、<u>大文字小文字を区別するオプション</u>、<u>複数行モードの有効化</u>等、様々なオプションを設定することができる。<br> | |||
これらのオプションは、パターンマッチングの動作を細かく制御するのに役立つ。<br> | |||
<br> | |||
マッチング結果は<code>QRegularExpressionMatch</code>オブジェクトとして返されて、マッチした部分文字列や位置情報等の詳細な情報を取得できる。<br> | |||
また、グループキャプチャを使用する場合は、パターン内の特定の部分にマッチした文字列を個別に取得することも可能である。<br> | |||
<br> | |||
正規表現のパターンが正しく構築されているかどうかを確認する場合は、<code>isValid</code>メソッドで事前に確認できる。<br> | |||
これは、実行時エラーを防ぐために重要である。<br> | |||
<br> | |||
<u>パフォーマンスにおいて、同じパターンを複数回使用する場合は、QRegularExpressionオブジェクトを再利用することを推奨する。</u><br> | |||
これにより、パターンの再コンパイルを避けることができる。<br> | |||
<br><br> | <br><br> | ||
== QRegularExpressionクラス (推奨) == | == QRegularExpressionクラス (推奨) == | ||
==== QRegularExpressionクラスとは ==== | |||
QRegularExpressionクラスは、PCREライブラリを基盤としており、以下に示すようなメリットがある。<br> | QRegularExpressionクラスは、PCREライブラリを基盤としており、以下に示すようなメリットがある。<br> | ||
* パフォーマンスの向上 | * パフォーマンスの向上 | ||
| 31行目: | 53行目: | ||
*: isValidメソッドを使用して、正規表現パターンの妥当性を確認することが推奨される。 | *: isValidメソッドを使用して、正規表現パターンの妥当性を確認することが推奨される。 | ||
<br> | <br> | ||
==== QRegularExpressionクラスの使用例 ==== | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QRegularExpression> | #include <QRegularExpression> | ||
| 161行目: | 184行目: | ||
QString code = "// This is a comment\nint x = 5;\n/* Multi\nline\ncomment */"; | QString code = "// This is a comment\nint x = 5;\n/* Multi\nline\ncomment */"; | ||
RegexExamples::extractComments(code); | RegexExamples::extractComments(code); | ||
</syntaxhighlight> | |||
<br> | |||
==== CaseInsensitiveOptionオプション ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegularExpression> | |||
#include <QDebug> | |||
QRegularExpression reCase("hello", QRegularExpression::CaseInsensitiveOption); | |||
QRegularExpressionMatch matchCase = reCase.match("HELLO world"); | |||
qDebug() << "Case insensitive match: " << matchCase.hasMatch(); // true | |||
</syntaxhighlight> | |||
<br> | |||
==== MultilineOptionオプション ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegularExpression> | |||
#include <QDebug> | |||
QString multilineText = "Line1\nLine2\nLine3"; | |||
QRegularExpression reMulti("^Line", QRegularExpression::MultilineOption); | |||
QRegularExpressionMatchIterator i = reMulti.globalMatch(multilineText); | |||
while (i.hasNext()) { | |||
QRegularExpressionMatch match = i.next(); | |||
qDebug() << "Found at position: " << match.capturedStart(); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== キャプチャグループ (数値指定) ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegularExpression> | |||
#include <QDebug> | |||
QRegularExpression reGroup("(\\w+)=(\\d+)"); | |||
QRegularExpressionMatch matchGroup = reGroup.match("age=25"); | |||
if (matchGroup.hasMatch()) { | |||
qDebug() << "Full match: " << matchGroup.captured(0); // "age=25" | |||
qDebug() << "First group: " << matchGroup.captured(1); // "age" | |||
qDebug() << "Second group: " << matchGroup.captured(2); // "25" | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 名前付きキャプチャグループ ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegularExpression> | |||
#include <QDebug> | |||
QRegularExpression reNamed("(?<key>\\w+)=(?<value>\\d+)"); | |||
QRegularExpressionMatch matchNamed = reNamed.match("count=42"); | |||
if (matchNamed.hasMatch()) { | |||
qDebug() << "Key: " << matchNamed.captured("key"); // "count" | |||
qDebug() << "Value: " << matchNamed.captured("value"); // "42" | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== マッチ位置の取得 ==== | |||
<syntaxhighlight lang="c++"> | |||
#include <QRegularExpression> | |||
#include <QDebug> | |||
QString text = "Hello, my email is test@example.com and phone is 123-456-7890"; | |||
// メールアドレスのパターン | |||
QRegularExpression emailRegex(R"(\b[\w\.-]+@[\w\.-]+\.\w+\b)"); | |||
// メールアドレスのマッチング | |||
QRegularExpressionMatch emailMatch = emailRegex.match(text); | |||
if (emailMatch.hasMatch()) { | |||
qDebug() << "Match text: " << emailMatch.captured(0); | |||
qDebug() << "Start position: " << emailMatch.capturedStart(); | |||
qDebug() << "Length: " << emailMatch.capturedLength(); | |||
qDebug() << "End position: " << emailMatch.capturedStart() + emailMatch.capturedLength(); | |||
} | |||
// 電話番号のパターン | |||
QRegularExpression phoneRegex(R"(\d{3}-\d{3}-\d{4})"); | |||
// 電話番号のマッチング | |||
QRegularExpressionMatch phoneMatch = phoneRegex.match(text); | |||
if (phoneMatch.hasMatch()) { | |||
qDebug() << "Match text: " << phoneMatch.captured(0); | |||
qDebug() << "Start position: " << phoneMatch.capturedStart(); | |||
qDebug() << "Length: " << phoneMatch.capturedLength(); | |||
qDebug() << "End position: " << phoneMatch.capturedStart() + phoneMatch.capturedLength(); | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 176行目: | 282行目: | ||
==== 基本的なマッチング ==== | ==== 基本的なマッチング ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// 単純な文字列パターンのマッチング | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 186行目: | 294行目: | ||
==== メタキャラクターを使用したパターン ==== | ==== メタキャラクターを使用したパターン ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// \d (数字) や \w (単語文字) 等の特殊文字の使用 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 195行目: | 305行目: | ||
==== 選択パターン ==== | ==== 選択パターン ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// | (パイプ) を使用した複数パターンの選択 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 205行目: | 317行目: | ||
==== グループ化とキャプチャ ==== | ==== グループ化とキャプチャ ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// ()を使用したグループ化と、capメソッドによるキャプチャ内容の取得 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 219行目: | 333行目: | ||
==== 量指定子の使用 ==== | ==== 量指定子の使用 ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// {n,m}形式での繰り返し回数の指定 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 228行目: | 344行目: | ||
==== 文字クラス ==== | ==== 文字クラス ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// []を使用した文字の範囲指定 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 237行目: | 355行目: | ||
==== テキスト置換 ==== | ==== テキスト置換 ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// replaceメソッドを使用したパターンマッチング置換 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 248行目: | 368行目: | ||
==== 否定文字クラス ==== | ==== 否定文字クラス ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// [^...]形式での否定パターン | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 257行目: | 379行目: | ||
==== 位置指定 ==== | ==== 位置指定 ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// ^ (行頭) や $ (行末) 等のアンカー | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 266行目: | 390行目: | ||
==== グリーディマッチとノングリーディマッチ ==== | ==== グリーディマッチとノングリーディマッチ ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// .と.?の違いによる最長一致と最短一致の制御 | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||
| 284行目: | 410行目: | ||
==== ワイルドカードモード ==== | ==== ワイルドカードモード ==== | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// ファイル名パターンマッチング等に使用する簡易的なパターン | |||
#include <QRegExp> | #include <QRegExp> | ||
#include <QDebug> | #include <QDebug> | ||