MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
Qtの基礎 - 正規表現のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
Qtの基礎 - 正規表現
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == <br><br> == QRegularExpressionクラス (推奨) == QRegularExpressionクラスは、PCREライブラリを基盤としており、以下に示すようなメリットがある。<br> * パフォーマンスの向上 : 内部実装が最適化されており、特に複雑なパターンマッチングでより高速に動作する。 * エラーハンドリングの改善 *: マッチング結果やエラー状態をより詳細に把握できるようになった。 *: 以前のQRegExpクラスでは、エラーが発生した際の情報が限定的であった。 * Unicode対応の強化 *: 現代的なテキスト処理に必要な、より包括的なUnicodeサポートを提供している。 <br> QRegularExpressionクラスを使用することにより、コードの保守性が向上して、より堅牢なテキスト処理機能を実装できる。<br> 移行を検討する場合は、既存のQRegExpクラスを使用したコードにおいて、新しい機能の実装時やコードのメンテナンス時に徐々にQRegularExpressionクラスへ移行することが推奨される。<br> <br> <br><br> == QRegExpクラス (非推奨) == QRegExpクラスには技術的な限界があり、PCRE (Perl Compatible Regular Expressions) との完全な互換性がなく、現代的な正規表現機能の一部をサポートしていない。<br> 例えば、先読み・後読みのような高度なパターンマッチング機能が制限されている。<br> <br> <u>Qt 5.12以降では、QRegExpクラスの代わりにQRegularExpressionクラスの使用が推奨されている。</u><br> <br> <u>※注意</u><br> <u>パターンに特殊文字を含める場合は、適切にエスケープする必要がある。</u><br> <u>パフォーマンスが重要な場合は、正規表現パターンを事前にコンパイルしておくことを推奨する。</u><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> == エラー == Qt 6において、<code>QRegExp</code>クラスを使用する場合、以下に示すエラーが発生する。<br> QRegExp was not declared in this scope <br> このエラーは、Qt 6では<code>QRegExp</code>クラスが非推奨となり、<code>QRegularExpression</code>クラスを使用する必要がある。<br> <br> <syntaxhighlight lang="c++"> #include <QRegularExpression> static QRegularExpression RegEx("[+-]"); QString coordinate = "111-222+333"; QStringList parts = coordinate.split(RegEx, Qt::SkipEmptyParts); </syntaxhighlight> <br> <code>QRegularExpression</code>クラスは、より強力で効率的な正規表現エンジンを提供しており、<code>QRegExp</code>クラスと比較していくつかの利点がある。<br> <br><br> {{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux |image=/resources/assets/MochiuLogo_Single_Blue.png }} __FORCETOC__ [[カテゴリ:Qt]]
Qtの基礎 - 正規表現
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse