2019-03-31 11:00:21 +00:00
---
title: 『新版Perl言語プログラミングレッスン入門編』第7 章・正規表現の基本
author: kazu634
date: 2008-02-18
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:3745;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
- Programming
---
< div class = "section" >
< p >
今日からは正規表現を頑張ってみます。基本的に正規表現のイロハは知っているので、Perlでどのように正規表現を使うのかを知ることが学習のメインポイントになると思っています。
< / p >
< p >
追記: 基本だから、結構知っていることばっかりだったかも。
< / p >
< p >
< a name = "seemore" > < / a >
< / p >
< h4 >
正規表現を用いて「マッチ」しているかどうかを調べる
< / h4 >
< p >
「=~」演算子を用いることで、「左辺の文字列が右辺のパターンに一致するかどうか」を調べます。
< / p >
< pre class = "syntax-highlight" >
< span class = "synStatement" > use strict< / span > ;
< span class = "synStatement" > use warnings< / span > ;
< span class = "synStatement" > my< / span > < span class = "synIdentifier" > $str< / span > = < span class = "synConstant" > 'yesterday'< / span > ;
< span class = "synStatement" > if< / span > (< span class = "synIdentifier" > $str< / span > =~< span class = "synStatement" > /< / span > < span class = "synConstant" > yes< / span > < span class = "synStatement" > /< / span > ) {
< span class = "synStatement" > print</ span > < span class = "synConstant" > " Match!</ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
} < span class = "synStatement" > else< / span > {
< span class = "synStatement" > print</ span > < span class = "synConstant" > " No Match!</ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
}
< / pre >
< h4 >
マッチした文字は変数 $& に格納される
< / h4 >
< pre class = "syntax-highlight" >
< span class = "synStatement" > use strict< / span > ;
< span class = "synStatement" > use warnings< / span > ;
< span class = "synStatement" > my< / span > < span class = "synIdentifier" > $str< / span > = < span class = "synConstant" > 'yesterday'< / span > ;
< span class = "synStatement" > if< / span > (< span class = "synIdentifier" > $str< / span > =~< span class = "synStatement" > /< / span > < span class = "synConstant" > yes< / span > < span class = "synStatement" > /< / span > ) {
< span class = "synStatement" > print</ span > < span class = "synConstant" > " </ span >< span class = "synIdentifier" > $& </ span >< span class = "synConstant" > --- Match!</ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
} < span class = "synStatement" > else< / span > {
< span class = "synStatement" > print</ span > < span class = "synConstant" > " No Match!</ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
}
< / pre >
< p >
このようにすると、変数 $& には「yes」が格納される。
< / p >
< h4 >
正規表現の「?」
< / h4 >
< p >
直前にあるのが「あっても、なくても構わない」ことを示します。
< / p >
< h4 >
マッチした文字列を置き換える
< / h4 >
< p >
「s///」を使うよ!
< / p >
< pre class = "syntax-highlight" >
< span class = "synStatement" > use strict< / span > ;
< span class = "synStatement" > use warnings< / span > ;
< span class = "synStatement" > my</ span > < span class = "synIdentifier" > $str</ span > = < span class = "synConstant" > " This is a cat. That is a cat, too.</ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
< span class = "synIdentifier" > $str< / span > =~ < span class = "synStatement" > s/< / span > < span class = "synConstant" > cat< / span > < span class = "synStatement" > /< / span > < span class = "synConstant" > dog< / span > < span class = "synStatement" > /< / span > ;
< span class = "synStatement" > print< / span > < span class = "synIdentifier" > $str< / span > ;
< / pre >
< p >
「s/cat/dog/」を使うと、最初にマッチしたcatだけを置換して、文字列の残りは無視されることに注意! ただし、次のようにすると全部置換してくれる。
< / p >
< pre class = "syntax-highlight" >
< span class = "synStatement" > use strict< / span > ;
< span class = "synStatement" > use warnings< / span > ;
< span class = "synStatement" > my</ span > < span class = "synIdentifier" > $str</ span > = < span class = "synConstant" > " This is a cat. That is a cat, too.</ span >< span class = "synSpecial" > \n</ span >< span class = "synConstant" > " </ span > ;
< span class = "synIdentifier" > $str< / span > =~ < span class = "synStatement" > s/< / span > < span class = "synConstant" > cat< / span > < span class = "synStatement" > /< / span > < span class = "synConstant" > dog< / span > < span class = "synStatement" > /g< / span > ;
< span class = "synStatement" > print< / span > < span class = "synIdentifier" > $str< / span > ;
< / pre >
2019-04-02 16:06:15 +00:00
< / div >