正規表現
[第]41[課では、]ファイル[操作]について学びました。
[今]、[私たちは][正規表現]――[テキスト処理の]「[スイスアーミーナイフ]」――を学びます。
[メールアドレスの検証]、[電話番号の抽出]、[テキストの置換]……[正規表現は、これらすべてに対して簡潔な解決策を提供します]。
1. [正規表現の概要]
(1) 1.1 [正規表現]とは何か?
[正規表現](Regex)は、[テキストパターンを記述するための言語]であり、[テキストの][一致]、[検索]、[置換]に[用いられる]。
[生活]クラス[比]:
- [ワイルドカード]
*→ [任意の文字に一致] - [正規表現] → [より強力なワイルドカード]
(2) 1.2 C++[正規表現ライブラリ]
C++11では[正規表現のサポートが導入され]、regexヘッダーファイル[に]含まれています。
[4つの主な]機能:
| 関数 | [機能] |
|---|---|
std::regex_match |
[完全一致] |
std::regex_search |
[検索] |
std::regex_replace |
[置換] |
std::regex_iterator |
[反復探索] |
2. [基本一致]
(1) 2.1 regex_match——[完全一致]
例:[携帯電話番号の確認] (難易度 ⭐)
▶ サンプル 1: [正規表現の応用] (難易度 ⭐)
#include iostream
#include regex
#include string
int main() {
std::string phone = "13812345678";
std::regex pattern("^1[3-9]\\d{9}$"); // ...
if (std::regex_match(phone, pattern)) {
std::cout << "..." << std::endl;
} else {
std::cout << "..." << std::endl;
}
return 0;
}
[実行結果]:
(2) 2.2 [正規表現の構文]
| [記号] | [説明] | 例 |
|---|---|---|
. |
[任意の文字] | a.c [一致] abc |
^ |
[先頭] | ^abc [「abc」で始まる] |
$ |
[末尾] | abc$ [「abc」で終わる] |
* |
0[1回以上] | a* [一致] aaa |
+ |
1[1回以上] | a+ [一致] aaa |
? |
0[次または]1[次] | a? [一致] a [または] `` |
{n} |
[ちょうど]n[次] | a{3} [一致] aaa |
[abc] |
[文字セット] | [abc] [一致] a [または] b [または] c |
[^abc] |
[文字セット以外] | [^abc] [「abc」以外の文字] |
\d |
[数字] | \d [一致] 0-9 |
\w |
[単語・文字] | \w [一致] a-z、A-Z、0-9、_ |
3. [検索と置換]
(1) 3.1 regex_search——[検索]
例:[メールアドレスの検索] (難易度 ⭐⭐)
#include iostream
### (2) ▶ 2:(⭐)
#include regex
#include string
int main() {
std::string text = ":abc@example.com test@gmail.com";
std::regex pattern("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
std::smatch match;
if (std::regex_search(text, match, pattern)) {
std::cout << ":" << match[0] << std::endl;
}
return 0;
}
(2) 3.2 regex_replace——[置換]
例:[携帯番号の中央4桁を非表示] (難易度 ⭐⭐)
#include iostream
#include regex
#include string
int main() {
std::string phone = "13812345678";
std::regex pattern("(\\d{3})\\d{4}(\\d{4})");
std::string result = std::regex_replace(phone, pattern, "$1****$2");
std::cout << ":" << result << std::endl;
return 0;
}
[実行結果]:
4. [反復探索]
(1) 4.1 regex_iterator
例:[すべてのメールアドレスを抽出する] (難易度 ⭐⭐⭐)
#include iostream
#include regex
#include string
int main() {
std::string text = ":abc@example.com test@gmail.com";
std::regex pattern("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
auto begin = std::sregex_iterator(text.begin(), text.end(), pattern);
auto end = std::sregex_iterator();
std::cout << ":" << std::endl;
for (auto it = begin; it != end; ++it) {
std::cout << it->str() << std::endl;
}
return 0;
}
[実行結果]:
abc@example.com
test@gmail.com
5. [グループ化とキャプチャ]
(1) 5.1 [グループ分け]
[括弧を使用] () [グループを作成]、[可能][部分文字列を抽出]。
例:[抽出日] (難易度 ⭐⭐)
#include iostream
#include regex
#include string
int main() {
std::string date = "2026-06-28";
std::regex pattern("(\\d{4})-(\\d{2})-(\\d{2})");
std::smatch match;
if (std::regex_match(date, match, pattern)) {
std::cout << ":" << match[1] << std::endl;
std::cout << ":" << match[2] << std::endl;
std::cout << ":" << match[3] << std::endl;
}
return 0;
}
[実行結果]:
6. [よくある]ユースケース
(1) 6.1 [検証]入力
| [シナリオ] | [正規表現] |
|---|---|
| [メールアドレス] | [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} |
| [携帯電話番号] | ^1[3-9]\d{9}$ |
| [身分証明書] | ^\d{17}[\dXx]$ |
| IPアドレス | ^(\d{1,3}\.){3}\d{1,3}$ |
(2) 6.2 [情報の抽出]
例:[HTMLから]リンクを[抽出する] (難易度 ⭐⭐⭐)
#include iostream
#include regex
#include string
int main() {
std::string html = "<a href=\"https://example.com\">Example</a>";
std::regex pattern("<a href=\"([^\"]+)\"");
std::smatch match;
if (std::regex_search(html, match, pattern)) {
std::cout << ":" << match[1] << std::endl;
}
return 0;
}
❓ よくある質問
std::regex コンストラクタの[処理が遅い]) - std::regex_constants::optimize [フラグ] [を使用する]Q:[エスケープ文字が多すぎる場合はどうすればよいですか]? A:[C++11の][生の]文字列リテラルを使用します:
// ...
std::regex pattern("\\\\d+");
// ...
std::regex pattern(R"(\d+)");
📖 まとめ
| [知識ポイント] | [要点] |
|---|---|
| regex_match | [完全一致] |
| regex_search | [検索] |
| regex_replace | [置換] |
| regex_iterator | [反復検索] |
| [グループ] | [使用] () [部分文字列の抽出] |
📝 練習問題
-
**初心者(難易度 ⭐):[使用] std::regex [判定] ある [string] が [パターン] 「[純数字]」[「^\d+$」] に [一致するかどうか]。テスト例:「123」、「12a3」、「abc」。
-
**中級(難易度 ⭐⭐):[用] regex_search [あるテキストからすべてのメールアドレスを抽出]([一致]
\w+@\w+\.\w+[パターン])。 -
上級(難易度 ⭐⭐⭐):[用] regex_replace [「[センシティブワードのフィルタリング]」関数を実装する]――[テキスト内の指定されたセンシティブワードを] * に置換する。[複数のセンシティブワードに対応]。
- [正規表現]:[パターンマッチ]string
- std::regex [正規表現の構築]オブジェクト
- std::regex_match [完全一致]
- std::regex_search [一致を検索]
- std::regex_replace [一致した内容を置換]
[次のレッスン]:マルチスレッド[基礎](#43)



