Skip to Content
『Learning Perl, 8th Edition』
book

『Learning Perl, 8th Edition』

by Randal L. Schwartz, brian d foy, Tom Phoenix
March 2025
Beginner to intermediate
398 pages
5h 33m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Perl, 8th Edition』

第8章 正規表現によるマッチング 正規表現によるマッチング

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

第7章では正規表現の世界を訪れた。今度は、その世界がPerlの世界にどのようにフィットするかを見てみよう。

m//と一致する。

のようにスラッシュのペアにパターンを入れるが、これは実は のパターンマッチ演算子のショートカットである。 演算子で見たように、任意の区切り文字のペアを選んで内容を引用することができる。そのため、同じ式を、区切り文字のペアを使用して、 、 、 、 と書くこともできるし、ペアを使用しない区切り文字を使用して、 、 、 、その他いろいろな書き方ができる。/fred/ m// qw// m(fred) m<fred> m{fred} m[fred] m,fred, m!fred! m^fred^

注

対にならない区切り文字とは、「左」と「右」の種類がないもので、両端に同じ記号が使われる。

区切り文字としてスラッシュを選択した場合、初期化m を省略することができる。Perlの人たちは余分な文字の入力を避けたがるので、ほとんどのパターン・マッチは/fred/ のようにスラッシュを使って書かれている。

もちろん、パターンに現れない区切り文字を賢く選ぶべきだ。パターンを普通のウェブURLの先頭にマッチさせたい場合、"http://" の初期化にマッチさせるために、/http:\/\// と書き始めるかもしれない。しかし、区切り文字にm%http://% を使った方が、読み取りも記述も保守もデバッグも楽になる。 区切り文字には波括弧を使うのが一般的だ。プログラマ用のテキストエディタを使っているのなら、おそらく波括弧から対応する閉じ括弧にジャンプする機能を備えているだろう。

対になる区切り文字を使用する場合、一般的にはパターン内部で区切り文字が対になるので、パターン内部での区切り文字の使用について心配する必要はない。つまり、m(fred(.*)barney) 、m{\w{2,}} 、m[wilma[\n \t]+betty] は、たとえパターンに引用文字が含まれていても、"left "には対応する "right "があるので、すべて問題ない。しかし、山括弧(< と> )は正規表現のメタキャラクタではないので、対にならないかもしれない。もしパターンがm{(\d+)\s*>=?\s*(\d+)} だった場合、山括弧でクォートすると、大なり記号をバックスラッシュしなければならなくなり、パターンを早々に終了させてしまうことになる。

マッチモディファイア

フラグと呼ばれる修飾文字がいくつかあり、区切り文字の直後にグループとして追加することができる。いくつかのフラグはパターンに適用され、いくつかはマッチ演算子の動作を変更する。

iによる大文字小文字の区別のないマッチング

大文字小文字を区別しないパターンマッチングを行い、fred やFred のように簡単にFRED にマッチできるようにするには、/i 修飾子を使う:

print "Would you like to play a game? ";
chomp($_ = <STDIN>);
if (/yes/i) {  # case-insensitive match
  print "In that case, I recommend that you go bowling.\n";
}

任意の文字に/sを付ける

デフォルトでは、ドット (.) は改行にはマッチしない。これは、ほとんどの「1行以内を検索する」パターンでは理にかなっている。文字列の中に改行があり、ドットが改行にマッチするようにしたい場合は、 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 9798341636231