簡易正規表現確認

概要

以下のようにすると正規表現が簡単に確認できる

perl -ne 'print if /正規表現を記載/' <テスト用ファイル>

実行例

事前準備

test.txt として以下のようなファイルを準備する。

www.example.com
testwww.example.com
test.www.example.com
test-www.example.com
www2.example.com
testwww2.example.com
test-www2.example.com
test.www2.example.com
実行例(その1)

以下のコマンドを実行(example.comにマッチ)

$ perl -ne 'print if /example\.com/' test.txt

結果は以下の通り

www.example.com
testwww.example.com
test.www.example.com
test-www.example.com
www2.example.com
testwww2.example.com
test-www2.example.com
test.www2.example.com
実行例(その2)

以下のコマンドを実行(www2にマッチ)

$ perl -ne 'print if /www2/' test.txt

結果は以下の通り

www2.example.com
testwww2.example.com
test-www2.example.com
test.www2.example.com
実行例(その3)

以下のコマンドを実行(サブドメインとなっているもののみにマッチ)

$ perl -ne 'print if /\.[a-zA-Z0-9\-]+\.example.com/' test.txt

結果は以下の通り

test.www.example.com
test.www2.example.com