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』

第7章 正規表現 正規表現

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

Perlは正規表現を強力にサポートしている。Perlの中のこのミニ言語は、パターンにマッチする文字列のファミリーを記述するためのタイトで強力な方法である。Perlの人気を支えている機能のひとつだ。

今日、多くの言語がこのような強力なツールに何らかの形でアクセスできるようになっている(おそらく「Perl互換正規表現」(PCRE)と呼ばれるもの)が、Perlはそのパワーと表現力において、いまだにそのほとんどを引き離している。

次の3章では、ほとんどのプログラミングで使う正規表現機能のほとんどを紹介する。この章では正規表現構文の基本を説明する。第8章では、マッチ演算子と、より洗練されたパターンの使い方を紹介する。最後に第9章では、パターンを使ってテキストを修正する方法を紹介する。

少なくともしばらくの間は、正規表現がこの言語のお気に入りのひとつになるだろう。しかし正規表現はタイトでコンパクトなので、慣れるまではイライラすることも多いだろう。 それは普通のことだ。これらの章を読みながら、例を試してみてほしい。より複雑なパターンは、先に読んだことを土台にしている。

シーケンス

Perlの正規表現は文字列にマッチするかしないかのどちらかである。部分一致というものはない。Perlはベストマッチを探すのではなく、パターンを満たす一番左の、一番長い部分文字列にマッチする。

注

他の言語の正規表現エンジンの中には、適合するものを発見した後に「より良い」適合を探すような、異なる動作をするものもある。詳しくはJeffrey FriedlのMastering Regular Expressionsを参照のこと。

最も単純なパターンはシーケンスである。リテラル文字を隣同士に並べることで、その順番の文字で部分文字列をマッチさせたいことを表明する。abba というシーケンスにマッチさせたい場合は、それをスラッシュで挟む:

$_ = "yabba dabba doo";
if (/abba/) {
  print "It matched!\n";
}

ifの式中のスラッシュはマッチ演算子である。これは、 の文字列にパターンを適用する。パターンはスラッシュの間の部分である。本書で値を囲む演算子を見たのはこれが初めてなので、奇妙に思えるかもしれない。 $_

パターンが$_ の文字列とマッチした場合、match演算子はtrueを返す。そうでない場合は偽を返す。マッチは、文字列の最初の位置でパターンを試すことで機能する。$_ の最初の文字はy であるが、パターンの最初の文字はa である。これらはマッチしないので、Perlは探し続ける。

次に、図7-1に示すように、マッチ演算子は一箇所スライドさせ、次の位置でマッ チを試みる。これはaにマッチする。次の文字、b にマッチしようとする。文字列の最初のb にマッチする。ここでもまずまずである。次に、2番目のb にマッチし、最後に最後のa にマッチする。マッチ演算子は文字列内のシーケンスを見つけたので、パターンはマッチする。

図7-1. パターンは文字列に沿って移動し、マッチするパターンを探す。

パターンが成功すると、match演算子はtrueを返す。 ...

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