■the m// Operator (Matching)

1. In scalar context, the operator returns true (1) if successful, false ("") otherwise.

ex.) If ($shire =~ /Baggins/) {..........}

2. In list context, m// returns a list of substrings matched by the capturing parentheses in the pattern.
In list context, m//g returns a list of all matches found.
If there are no capturing parentheses within the /g pattern, then the complete matches are returned. If there are capturing parentheses, then only the strings captured are returned.
If the match fails in list context, a null list is returned.
If the the match succeeds in list context, but there were no capturing parentheses nor /g, a list value (1) is returned.

ex.) If (($key, $value) = $string =~ /(\w+): (.*)/) {..........}
ex.) If (@perls = $paragraph =~ /perl/gi) {..........}

3. その他

ex.) %hash = $string =~ /(\w+)=(\w+)/g;

■the s/// Operator (Substitution)

1. The return value of an s/// operation (in scalar and list contexts alike) is the number of times it succeeded (which can be more than once if used with the /g modifier).
On failure, since it substituted zero times, it returns false(""), which is numerically equivalent to 0.

ex.) If ($lotr =~ s/Bilbo/Frodo/) {..........}
ex.) $change_count = $lotr =~ s/Bilbo/Frodo/g;

2. その他

ex.) s/revision|version|release/\u$&/g; #大文字化
ex.) s{version\s+([0-9.]+)}{$Names{$1}?"The $Names{$1} release":$&}xge; #コードの実行結果が置換文字列として使われる
ex.) ($date = $pp_data->[4]) =~ s/^\d{4}\///g; #shiromuku(r2)BBS
ex.) for (@new = @old) {s/Bilbo/Frodo/} #@oldの内容を変えたくない場合
ex.) for ($new = $old) {s/^\s+//; s/\s+$//; s/\s+/ /;} #$newの先頭の空白文字を削除、最後の空白文字を削除、中間にある空白文字の連続を一個の空白文字に変える
ex.) 1 while $number =~ s/(\d)(\d\d\d)(?!\d)/$1,$2/; #数字にコンマ付加
ex.) 1 while $string =~ s/\b(\w+) \1\b/$1/gi; #"this this"→"this"

■Capturing and Clustering

ex.) /foo((?-i)bar)/i #"bar" must be lower case(Capturingあり)
   /foo(?-i:bar)/i #"bar" must be lower case(Capturingなし)


ex.) /pro(b|n|r|l)ate/ #(Capturingあり)
   /pro(?:b|n|r|l)ate/ #(Capturingなし)

ex.) /com(pound|)/ #Matches "compound" or "com"("com"の場合$1はdefined null string)
   /com(pound)?/ #Matches "compound" or "com"("com"の場合$1はundefined)


 ex.) $cat = qr/\d+/;
if ($filename =~ /^($cat)\_(\w+)\.cgi$/o) {..........}

ex.) @regexes = ();
@regexes = map {qr/$_/} @patterns;
foreach $item(@data) {
foreach $re(@regexes) {
if ($item =~ /$re/) {..........}

■Lookaround Assertions
ex.) 1 while $string =~ s/\b(\w+)\s(?=\1\b)//gi; #positive lookahead("this this"→"this")
ex.) 1 while $string =~ s/\b(\w+)\s(?=\1\b(?!'\w))//gi; #negative lookahead("The clothes you DON DON't fit."を回避)
ex.) s/(?<=c)ei/ie/g #positive lookbehind (前に"c"がある"ei"を"ie"に置換
ex.) s/(?<!c)ei/ie/g #negative lookbehind (前に"c"がない"ei"を"ie"に置換

■the /e modifier

ex.) s/(\d+)/$1*2/e; #"42"を"84" に置換
ex.) s/\b(\d+\.?\d*)C\b/int($1*1.8+32) . "F"/e; #233C → 451F
ex.) s/^(\d+)(?=:)/$1+100/e; #行の最初の数字(後に":"が付いている)を100増加
ex.) s/(\$\w+)/$1/eg; #スカラー変数を展開
ex.) $_ = "I have 4+19 dollars and 8/2 cents.\n";
print; #"I have 23 dollars and 4 cents."になる
2003年10月27日(月) No.45


Icon Icon
投稿キー ナニ・ュ。シを右に記入して下さい