boostの正規表現ライブラリを使う

最終更新日

Comments: 3

boostの正規表現ライブラリ boost::regex を使ったサンプルです。
boost::regex は以下のコマンドでインストールすることができます。

[bash]
$ sudo aptitude install libboost-regex-dev
[/bash]

サンプル regextest.cpp を準備します。これは正規表現リプレイサです。

[cpp]

include <iostream>

include <string>

include <boost/regex.hpp>

// usage: a.out pattern replace-to

using namespace std;

int main(int c, char** v)
{
if (c != 3) return 1;

boost::regex ex(v[1]); // 正規表現オブジェクト
char buf[256];         // 読み取り用のバッファ

while (cin.getline(buf, sizeof(buf)))
cout << boost::regex_replace(string(buf), ex, v[2]) << endl;

return 0;
}
[/cpp]

ビルドします。

[bash]
$ g++ -lboost_regex regextest.cpp
[/bash]

実行します。

[bash]

先頭の二文字を入れ替える

$ ls / | ./a.out ‘^(.)(.)’ ‘$2$1’
ibn
obot
edv
tec
ohme
niitrd.img
ilb
olst+found
nmt
pot
rpoc
orot
bsin
eslinux
rsv
yss
mtp
sur
avr
mvlinuz
[/bash]

簡単ですね。

佐世保のシステムエンジニアです。詳しいプロフィールやこのブログについてはこちらをご覧ください。

3件のフィードバック

C#とC++/CLIで正規表現ライブラリを使う | dyama's home page へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

コメントする

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください