Skip to Content
Java クックブック、第 4 版
book

Java クックブック、第 4 版

by Ian F. Darwin
May 2025
Intermediate to advanced
638 pages
10h 22m
Japanese
O'Reilly Media, Inc.
Content preview from Java クックブック、第 4 版

第4章 正規表現によるパターンマッチング 正規表現によるパターンマッチング

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

4.0 はじめに

あなたが数年間インターネットを利用していて、自分(あるいは弁護士や検察)がコピーを必要とする場合に備えて、すべての通信を忠実に保存していたとしよう。その結果、保存したメール専用の5GBのディスク・パーティションができた。さらに、その中のどこかにアンジーかアンジーという名前の誰かからの電子メール・メッセージがあることを覚えているとしよう。それともアンジーだったか?しかし、それを何と呼び出したか、どこに保存したかは覚えていない。当然、探さなければならない。

しかし、ワープロで15,000,000の文書をすべて開こうとする人がいる一方で、私は単純なコマンドひとつで発見する。正規表現をサポートしているシステムであれば、いくつかの方法でパターンを検索することができる。最も単純に理解できるのは

Angie|Anjie|Angy

というのは、おそらくどのバリエーションでも検索できるという意味だろう。より簡潔な形(より多く考え、より少なくタイプする)は次の通りである:

An[^ dn]

構文は、この章を読み進めていくうちに明らかになっていくだろう。簡単に言うと、"A "と "n "はそれ自体が一致し、事実上 "An "で始まる単語を発見する。一方、[^ dn] の暗号は、"An "の後にスペース文頭のごく一般的な英単語 "an "を排除する)、"d"(一般的な単語 "and "を排除する)、"n"("Anne"、"Announcing "などを排除する)以外の文字が続くことを要求する。あなたのワープロはもうスプラッシュ・スクリーンを卒業しただろうか?まあ、そんなことはどうでもいい。なぜなら、私はすでに行方不明のファイルを発見しているからだ。答えを見つけるには、このコマンドを入力するだけだ:

grep 'An[^ dn]' *

正規表現、略して正規表現(regex)は、テキスト中のマッチするパターンを簡潔かつ正確に指定するものである。 正規表現を考える良い方法のひとつは、文字列中の文字パターンをマッチングするための小さな言語である。 正規表現APIは、正規表現をマッチングするためのインタプリタである。

正規表現の威力を示すもう1つの例として、何百ものファイルを一括更新する問題を考えてみよう。私がJavaを始めたころ、配列参照を宣言する構文はbaseType arrayVariableName[] 。例えば、すべてのプログラムのmainメソッドのような、配列引数を持つメソッドは、一般的にこのように書かれていた:

public static void main(String args[]) {

しかし時が経つにつれ、Java言語の責任者たちは、このようにbaseType[] arrayVariableName と書いた方が良いということが分かってきた:

public static void main(String[] args) {

これは、型の「配列らしさ」をローカル引数の名前ではなく、型自体に関連付けるもので、コンパイラーはどちらのモードも受け入れるので、Javaのスタイルとしてはこちらの方が優れている。私は、古い方法で書かれたmain を新しい方法に変更したかった。先に説明したgrepユーティリティでパターン main(String [a-z] ...

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

最新の Java レシピ

最新の Java レシピ

Ken Kousen
Javaによる関数型プログラミング ―Java 8ラムダ式とStream

Javaによる関数型プログラミング ―Java 8ラムダ式とStream

Venkat Subramaniam, 株式会社プログラミングシステム社
MySQL クックブック、第 4 版

MySQL クックブック、第 4 版

Sveta Smirnova, Alkin Tezuysal

Publisher Resources

ISBN: 9798341650336