Skip to Content
初めてのPerl 第7版
book

初めてのPerl 第7版

by Randal L. Schwartz, brian d foy, Tom Phoenix, 近藤 嘉雪, 嶋田 健志
January 2018
Intermediate to advanced
408 pages
9h 18m
Japanese
O'Reilly Japan, Inc.
Content preview from 初めてのPerl 第7版
164
9 章 正規表現によるテキスト処理
(my $copy = $original) =~ s/\d+ ribs?/10 ribs/;
これは少しわかりにくいかもしれません。なぜなら、置換演算子の対象は代入先の変数
$copy
あり、実際には
$copy
の値が変更されるということを、多くの人が忘れているからです。
Perl 5.14
では、この仕組みを変える
/r
修飾子が導入されました。通常は、
s///
の値は置換が行われた回数
ですが、
/r
修飾子を指定すると、元の文字列は変更せずに、置換の結果を値として返すようにな
ります。
use v5.14;
my $copy = $original =~ s/\d+ ribs?/10 ribs/r;
これは
1
つ前の例とほとんど同じに見えますが、カッコがありません。しかし、このケースで
は、逆順に処理が行われるのです。まず最初に置換が行われ、その次に代入が行われるのです。
2
章(と
perlop
ドキュメント)には、優先順位表があるので確かめてみましょう。
=~
のほうが
=
り優先度が高くなっています。
9.1.6
 大文字と小文字の変換
置換を行う際に、置き換える単語の先頭を大文字にしたい(あるいは、したくない)ことがあり
ます。
Perl
では、バックスラッシュエスケープを使えば、このような仕事は朝飯前です。
\U
エス
ケープは、その後ろに続くものをすべて強制的に大文字に変換します。
$_ = "I saw Barney with Fred.";
s/(fred|barney)/\U$1/gi; ...
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

Pythonではじめる機械学習 ―scikit-learnで学ぶ特徴量エンジニアリングと機械学習の基礎

Pythonではじめる機械学習 ―scikit-learnで学ぶ特徴量エンジニアリングと機械学習の基礎

Andreas C. Muller, Sarah Guido, 中田 秀基

Publisher Resources

ISBN: 9784873118246Other