[perldocjp-cvs 2037] CVS update: docs/perl/5.22.1

Back to archive index

argra****@users***** argra****@users*****
2017年 3月 28日 (火) 04:19:12 JST


Index: docs/perl/5.22.1/perlrequick.pod
diff -u /dev/null docs/perl/5.22.1/perlrequick.pod:1.1
--- /dev/null	Tue Mar 28 04:19:12 2017
+++ docs/perl/5.22.1/perlrequick.pod	Tue Mar 28 04:19:12 2017
@@ -0,0 +1,1368 @@
+
+=encoding euc-jp
+
+=head1 NAME
+
+=begin original
+
+perlrequick - Perl regular expressions quick start
+
+=end original
+
+perlrequick - Perl 正規表現のクイックスタート
+
+=head1 DESCRIPTION
+
+=begin original
+
+This page covers the very basics of understanding, creating and
+using regular expressions ('regexes') in Perl.
+
+=end original
+
+このページは、Perl の正規表現 ('regexes') の理解、作成、使用の基本中の
+基本に対応しています。
+
+=head1 The Guide
+
+(ガイド)
+
+=head2 Simple word matching
+
+(単純な単語のマッチング)
+
+=begin original
+
+The simplest regex is simply a word, or more generally, a string of
+characters.  A regex consisting of a word matches any string that
+contains that word:
+
+=end original
+
+最も単純な正規表現は単なる単語、より一般的には文字の並びです。
+正規表現は単語を構成する任意の文字列にマッチングする単語からなります:
+
+    "Hello World" =~ /World/;  # matches
+
+=begin original
+
+In this statement, C<World> is a regex and the C<//> enclosing
+C</World/> tells Perl to search a string for a match.  The operator
+C<=~> associates the string with the regex match and produces a true
+value if the regex matched, or false if the regex did not match.  In
+our case, C<World> matches the second word in C<"Hello World">, so the
+expression is true.  This idea has several variations.
+
+=end original
+
+この文で、C<World> は正規表現であり、 C<//> で囲まれた C</World/> は
+Perl に対してマッチングのために文字列を検索することを指示します。
+C<=~> という演算子は正規表現にマッチングする文字列に結び付けられ、
+正規表現がマッチングすれば真の値を生成し、マッチングしなければ偽となります。
+この例では、C<World> は C<"Hello World"> の二番目の単語にマッチングするので、
+式は真となります。
+この考え方にはいくつかのバリエーションがあります。
+
+=begin original
+
+Expressions like this are useful in conditionals:
+
+=end original
+
+以下のような式は条件文で便利です:
+
+    print "It matches\n" if "Hello World" =~ /World/;
+
+=begin original
+
+The sense of the match can be reversed by using C<!~> operator:
+
+=end original
+
+マッチングの成否の意味を反転する演算子 C<!~> があります:
+
+    print "It doesn't match\n" if "Hello World" !~ /World/;
+
+=begin original
+
+The literal string in the regex can be replaced by a variable:
+
+=end original
+
+正規表現中のリテラル文字列は変数に置き換えることもができます:
+
+    $greeting = "World";
+    print "It matches\n" if "Hello World" =~ /$greeting/;
+
+=begin original
+
+If you're matching against C<$_>, the C<$_ =~> part can be omitted:
+
+=end original
+
+C<$_> に対してマッチングを行う場合、C<$_ =~> の部分は省略できます:
+
+    $_ = "Hello World";
+    print "It matches\n" if /World/;
+
+=begin original
+
+Finally, the C<//> default delimiters for a match can be changed to
+arbitrary delimiters by putting an C<'m'> out front:
+
+=end original
+
+最後に、マッチングのための C<//> のデフォルトデリミタは C<'m'> を
+前置することにより任意のものにすることができます:
+
+=begin original
+
+    "Hello World" =~ m!World!;   # matches, delimited by '!'
+    "Hello World" =~ m{World};   # matches, note the matching '{}'
+    "/usr/bin/perl" =~ m"/perl"; # matches after '/usr/bin',
+                                 # '/' becomes an ordinary char
+
+=end original
+
+    "Hello World" =~ m!World!;   # マッチングする; デリミタは '!'
+    "Hello World" =~ m{World};   # マッチングする; 組になっている '{}' に注意
+    "/usr/bin/perl" =~ m"/perl"; # 'usr/bin' の後にマッチングする
+                                 # '/' は普通の文字になっている
+
+=begin original
+
+Regexes must match a part of the string I<exactly> in order for the
+statement to be true:
+
+=end original
+
+正規表現は、文が真となるためには I<正確に> 順序通りに文字列の
+一部としてマッチングしなければなりません。
+
+=begin original
+
+    "Hello World" =~ /world/;  # doesn't match, case sensitive
+    "Hello World" =~ /o W/;    # matches, ' ' is an ordinary char
+    "Hello World" =~ /World /; # doesn't match, no ' ' at end
+
+=end original
+
+    "Hello World" =~ /world/;  # マッチングしない; 大文字小文字は区別する
+    "Hello World" =~ /o W/;    # マッチングする; ' ' は普通の文字
+    "Hello World" =~ /World /; # マッチングしない; 末尾に ' ' はない
+
+=begin original
+
+Perl will always match at the earliest possible point in the string:
+
+=end original
+
+Perl は常に文字列の中で最初に現れるものをマッチングしようとします:
+
+=begin original
+
+    "Hello World" =~ /o/;       # matches 'o' in 'Hello'
+    "That hat is red" =~ /hat/; # matches 'hat' in 'That'
+
+=end original
+
+    "Hello World" =~ /o/;       # 'Hello' の 'o' にマッチング
+    "That hat is red" =~ /hat/; # 'That' の中の 'hat' にマッチング
+
+=begin original
+
+Not all characters can be used 'as is' in a match.  Some characters,
+called B<metacharacters>, are reserved for use in regex notation.
+The metacharacters are
+
+=end original
+
+すべての文字がマッチングにおいて'あるがまま'(as is) に使われるのでは
+ありません。
+I<メタ文字> と呼ばれる幾つかの文字が正規表現の記述に使うために
+予約されています。
+メタ文字には以下のものがあります
+
+    {}[]()^$.|*+?\
+
+=begin original
+
+A metacharacter can be matched by putting a backslash before it:
+
+=end original
+
+メタ文字はバックスラッシュを前置することによってマッチングさせられます:
+
+=begin original
+
+    "2+2=4" =~ /2+2/;    # doesn't match, + is a metacharacter
+    "2+2=4" =~ /2\+2/;   # matches, \+ is treated like an ordinary +
+    'C:\WIN32' =~ /C:\\WIN/;                       # matches
+    "/usr/bin/perl" =~ /\/usr\/bin\/perl/;  # matches
+
+=end original
+
+    "2+2=4" =~ /2+2/;    # マッチングしない; + はメタ文字
+    "2+2=4" =~ /2\+2/;   # マッチングする; \+ 普通の + のように扱われる
+    'C:\WIN32' =~ /C:\\WIN/;                       # マッチングする
+    "/usr/bin/perl" =~ /\/usr\/bin\/perl/;  # マッチングする
+
+=begin original
+
+In the last regex, the forward slash C<'/'> is also backslashed,
+because it is used to delimit the regex.
+
+=end original
+
+最後の正規表現では、スラッシュ C<'/'> もまたバックスラッシュが
+つけられています;
+なぜなら、それが正規表現のデリミタとして使われているからです。
+
+=begin original
+
+Non-printable ASCII characters are represented by B<escape sequences>.
+Common examples are C<\t> for a tab, C<\n> for a newline, and C<\r>
+for a carriage return.  Arbitrary bytes are represented by octal
+escape sequences, e.g., C<\033>, or hexadecimal escape sequences,
+e.g., C<\x1B>:
+
+=end original
+
+印字できない ASCII 文字は B<エスケープシーケンス> によって表現されます。
+一般的な例では、タブを表す C<\t>、改行を表す C<\n>、復帰を表す C<\r> が
+あります。
+任意のバイトは 8 進エスケープシーケンス (例えば C<\033>) あるいは
+16 進エスケープシーケンス (例えば C<\x1B>) で表現できます:
+
+=begin original
+
+    "1000\t2000" =~ m(0\t2)  # matches
+    "cat" =~ /\143\x61\x74/  # matches in ASCII, but 
+                             # a weird way to spell cat
+
+=end original
+
+    "1000\t2000" =~ m(0\t2)  # マッチングする
+    "cat" =~ /\143\x61\x74/  # ASCII でマッチングするが、
+                             # cat を綴る変な方法
+
+=begin original
+
+Regexes are treated mostly as double-quoted strings, so variable
+substitution works:
+
+=end original
+
+正規表現はほとんどの場合においてダブルクォートで囲まれた文字列のように
+扱われるので、変数置換は動作します:
+
+=begin original
+
+    $foo = 'house';
+    'cathouse' =~ /cat$foo/;   # matches
+    'housecat' =~ /${foo}cat/; # matches
+
+=end original
+
+    $foo = 'house';
+    'cathouse' =~ /cat$foo/;   # マッチングする
+    'housecat' =~ /${foo}cat/; # マッチングする
+
+=begin original
+
+With all of the regexes above, if the regex matched anywhere in the
+string, it was considered a match.  To specify I<where> it should
+match, we would use the B<anchor> metacharacters C<^> and C<$>.  The
+anchor C<^> means match at the beginning of the string and the anchor
+C<$> means match at the end of the string, or before a newline at the
+end of the string.  Some examples:
+
+=end original
+
+これまでの正規表現では、文字列のどこかでマッチングすればマッチングしたと
+みなしてきました。
+文字列の I<どこで> 正規表現がマッチングするのかを指定するには、
+I<アンカー> メタ文字である C<^> と C<$> を使います。
+アンカー C<^> は文字列の先頭でマッチングすることを意味し、アンカー C<$> は
+文字列の末尾(あるいは文字列の末尾にある改行の前) でマッチングすることを
+意味します。
+いくつか例を挙げます:
+
+=begin original
+
+    "housekeeper" =~ /keeper/;         # matches
+    "housekeeper" =~ /^keeper/;        # doesn't match
+    "housekeeper" =~ /keeper$/;        # matches
+    "housekeeper\n" =~ /keeper$/;      # matches
+    "housekeeper" =~ /^housekeeper$/;  # matches
+
+=end original
+
+    "housekeeper" =~ /keeper/;         # マッチングする
+    "housekeeper" =~ /^keeper/;        # マッチングしない
+    "housekeeper" =~ /keeper$/;        # マッチングする
+    "housekeeper\n" =~ /keeper$/;      # マッチングする
+    "housekeeper" =~ /^housekeeper$/;  # マッチングする
+
+=head2 Using character classes
+
+(文字クラスを使う)
+
+=begin original
+
+A B<character class> allows a set of possible characters, rather than
+just a single character, to match at a particular point in a regex.
+Character classes are denoted by brackets C<[...]>, with the set of
+characters to be possibly matched inside.  Here are some examples:
+
+=end original
+
+B<文字クラス> は正規表現の特定の場所においてマッチングする可能性のある文字の
+集合です(単一の文字ではありません)。
+文字クラスはブラケット C<[...]> で表現され、マッチングする可能性のある文字の
+集合はその内側に置かれます。
+以下はその例です:
+
+=begin original
+
+    /cat/;            # matches 'cat'
+    /[bcr]at/;        # matches 'bat', 'cat', or 'rat'
+    "abc" =~ /[cab]/; # matches 'a'
+
+=end original
+
+    /cat/;            # 'cat' にマッチングする
+    /[bcr]at/;        # 'bat', 'cat', 'rat' のいずれかにマッチングする
+    "abc" =~ /[cab]/; # 'a' にマッチングする
+
+=begin original
+
+In the last statement, even though C<'c'> is the first character in
+the class, the earliest point at which the regex can match is C<'a'>.
+
+=end original
+
+最後の文において、C<'c'> がクラスの最初の文字であるにもかかわらず
+正規表現がマッチングすることのできる最初の位置にある文字である C<'a'> が
+マッチングします。
+
+=begin original
+
+    /[yY][eE][sS]/; # match 'yes' in a case-insensitive way
+                    # 'yes', 'Yes', 'YES', etc.
+    /yes/i;         # also match 'yes' in a case-insensitive way
+
+=end original
+
+    /[yY][eE][sS]/; # 大文字小文字を無視して 'yes' にマッチングする
+                    # 'yes', 'Yes', 'YES' など。
+    /yes/i;         # これも大文字小文字を無視して 'yes' にマッチングする
+
+=begin original
+
+The last example shows a match with an C<'i'> B<modifier>, which makes
+the match case-insensitive.
+
+=end original
+
+最後の例は大文字小文字を無視してマッチングするようにする
+C<'i'> B<修飾子> (modifier) を使ったマッチングを示しています。
+
+=begin original
+
+Character classes also have ordinary and special characters, but the
+sets of ordinary and special characters inside a character class are
+different than those outside a character class.  The special
+characters for a character class are C<-]\^$> and are matched using an
+escape:
+
+=end original
+
+文字クラスも普通の文字と特殊文字がありますが、文字クラスの内側での
+普通の文字と特殊文字は、文字クラスの外側の物とは違います。
+文字クラスのために特殊な文字は C<-]\^$> で、エスケープを使って
+マッチングされます:
+
+=begin original
+
+   /[\]c]def/; # matches ']def' or 'cdef'
+   $x = 'bcr';
+   /[$x]at/;   # matches 'bat, 'cat', or 'rat'
+   /[\$x]at/;  # matches '$at' or 'xat'
+   /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'
+
+=end original
+
+   /[\]c]def/; # ']def' または 'cdef' にマッチング
+   $x = 'bcr';
+   /[$x]at/;   # 'bat', 'cat', 'rat' にマッチング
+   /[\$x]at/;  # '$at' または 'xat' にマッチング
+   /[\\$x]at/; # '\at', 'bat, 'cat', 'rat' にマッチング
+
+=begin original
+
+The special character C<'-'> acts as a range operator within character
+classes, so that the unwieldy C<[0123456789]> and C<[abc...xyz]>
+become the svelte C<[0-9]> and C<[a-z]>:
+
+=end original
+
+特殊文字 C<'-'> は文字クラスの中で範囲演算子として振舞うので、
+C<[0123456789]> や C<[abc...xyz]> のような
+見づらいものはすっきりとした C<[0-9]> であるとか C<[a-z]> のように
+書き換えられます:
+
+=begin original
+
+    /item[0-9]/;  # matches 'item0' or ... or 'item9'
+    /[0-9a-fA-F]/;  # matches a hexadecimal digit
+
+=end original
+
+    /item[0-9]/;  # 'item0' ... 'item9' にマッチングする
+    /[0-9a-fA-F]/;  # 16 進数にマッチングする
+
+=begin original
+
+If C<'-'> is the first or last character in a character class, it is
+treated as an ordinary character.
+
+=end original
+
+C<'-'> が文字クラスの中の最初か最後の文字であった場合、通常の文字として
+扱われます。
+
+=begin original
+
+The special character C<^> in the first position of a character class
+denotes a B<negated character class>, which matches any character but
+those in the brackets.  Both C<[...]> and C<[^...]> must match a
+character, or the match fails.  Then
+
+=end original
+
+文字クラスの先頭の位置にある特殊文字 C<^> は B<反転文字クラス> を表し、
+ブラケットの中にない文字にマッチングします。
+C<[...]> と C<[^...]> の両方とも、一つの文字にマッチングせねばならず、
+そうでない場合にはマッチングは失敗します。
+ですから
+
+=begin original
+
+    /[^a]at/;  # doesn't match 'aat' or 'at', but matches
+               # all other 'bat', 'cat, '0at', '%at', etc.
+    /[^0-9]/;  # matches a non-numeric character
+    /[a^]at/;  # matches 'aat' or '^at'; here '^' is ordinary
+
+=end original
+
+    /[^a]at/;  # 'aat' や 'at' にはマッチングしないが、その他の
+               # 'bat', 'cat, '0at', '%at' などにはマッチングする
+    /[^0-9]/;  # 数字以外にマッチングする
+    /[a^]at/;  # 'aat' か '^at'にマッチングする; ここでは '^' は通常の文字
+
+=begin original
+
+Perl has several abbreviations for common character classes. (These
+definitions are those that Perl uses in ASCII-safe mode with the C</a> modifier.
+Otherwise they could match many more non-ASCII Unicode characters as
+well.  See L<perlrecharclass/Backslash sequences> for details.)
+
+=end original
+
+Perl は一般的な文字クラスの略記法を持っています。
+(これらの定義は Perl が C</a> 修飾子によって ASCII 安全モードを
+使っているときのものです。
+さもなければもっと多くの非 ASCII の Unicode 文字に
+マッチングするかもしれません。
+詳しくは L<perlrecharclass/Backslash sequences> を参照してください。)
+
+=over 4
+
+=item *
+
+=begin original
+
+\d is a digit and represents
+
+=end original
+
+\d は数字で、以下のものを表します
+
+    [0-9]
+
+=item *
+
+=begin original
+
+\s is a whitespace character and represents
+
+=end original
+
+\s は空白文字で、以下のものを表します
+
+    [\ \t\r\n\f]
+
+=item *
+
+=begin original
+
+\w is a word character (alphanumeric or _) and represents
+
+=end original
+
+\w は単語を構成する文字(英数字 と _)で、以下のものを表します
+
+    [0-9a-zA-Z_]
+
+=item *
+
+=begin original
+
+\D is a negated \d; it represents any character but a digit
+
+=end original
+
+\D は \d の否定形です; 数字以外の文字を表します
+
+    [^0-9]
+
+=item *
+
+=begin original
+
+\S is a negated \s; it represents any non-whitespace character
+
+=end original
+
+\S は \s の否定形です; 非空白文字を表します
+
+    [^\s]
+
+=item *
+
+=begin original
+
+\W is a negated \w; it represents any non-word character
+
+=end original
+
+\W は \w の否定形です; 単語を構成しない文字を表します
+
+    [^\w]
+
+=item *
+
+=begin original
+
+The period '.' matches any character but "\n"
+
+=end original
+
+ピリオド '.' は "\n" 以外の任意の文字にマッチングします
+
+=back
+
+=begin original
+
+The C<\d\s\w\D\S\W> abbreviations can be used both inside and outside
+of character classes.  Here are some in use:
+
+=end original
+
+C<\d\s\w\D\S\W> の省略記法は文字クラスの内側でも外側でも使うことができます。
+以下はその例です:
+
+=begin original
+
+    /\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format
+    /[\d\s]/;         # matches any digit or whitespace character
+    /\w\W\w/;         # matches a word char, followed by a
+                      # non-word char, followed by a word char
+    /..rt/;           # matches any two chars, followed by 'rt'
+    /end\./;          # matches 'end.'
+    /end[.]/;         # same thing, matches 'end.'
+
+=end original
+
+    /\d\d:\d\d:\d\d/; # hh:mm:ss 形式の時間表記にマッチング
+    /[\d\s]/;         # 数字または空白にマッチング
+    /\w\W\w/;         # 非単語文字が続きさらに単語文字が続く
+                      # 単語文字にマッチング
+    /..rt/;           # 'rt' が続く任意の二文字にマッチング
+    /end\./;          # 'end.' にマッチング
+    /end[.]/;         # 同じこと; 'end.' にマッチング
+
+=begin original
+
+The S<B<word anchor> > C<\b> matches a boundary between a word
+character and a non-word character C<\w\W> or C<\W\w>:
+
+=end original
+
+S<B<語アンカー> > (word anchor) C<\b> はこれは単語を構成する文字と単語を
+構成しない文字の間 C<\w\W> や C<\W\w> の境界にマッチングします:
+
+=begin original
+
+    $x = "Housecat catenates house and cat";
+    $x =~ /\bcat/;  # matches cat in 'catenates'
+    $x =~ /cat\b/;  # matches cat in 'housecat'
+    $x =~ /\bcat\b/;  # matches 'cat' at end of string
+
+=end original
+
+    $x = "Housecat catenates house and cat";
+    $x =~ /cat/;    # 'housecat' の cat にマッチング
+    $x =~ /\bcat/;  # 'catenates' の cat にマッチング
+    $x =~ /cat\b/;  # 'housecat' の cat にマッチング
+    $x =~ /\bcat\b/;  # 文字列の終端の'cat'にマッチング
+
+=begin original
+
+In the last example, the end of the string is considered a word
+boundary.
+
+=end original
+
+最後の例では、文字列の終端は単語境界として認識されています。
+
+=begin original
+
+For natural language processing (so that, for example, apostrophes are
+included in words), use instead C<\b{wb}>
+
+=end original
+
+    "don't" =~ / .+? \b{wb} /x;  # matches the whole string
+
+=head2 Matching this or that
+
+(あれやこれやにマッチングする)
+
+=begin original
+
+We can match different character strings with the B<alternation>
+metacharacter C<'|'>.  To match C<dog> or C<cat>, we form the regex
+C<dog|cat>.  As before, Perl will try to match the regex at the
+earliest possible point in the string.  At each character position,
+Perl will first try to match the first alternative, C<dog>.  If
+C<dog> doesn't match, Perl will then try the next alternative, C<cat>.
+If C<cat> doesn't match either, then the match fails and Perl moves to
+the next position in the string.  Some examples:
+
+=end original
+
+異なる文字列を B<選択> メタ文字 C<'|'> によって行えます。
+C<dog> または C<cat> にマッチングさせるには、正規表現を
+C<dog|cat> のようにします。
+以前述べた通り、Perlは文字列の可能な限り最も早い位置でマッチングを
+行おうとします。
+それぞれの文字位置で、Perlはまずはじめに最初の選択である C<dog> に
+マッチングさせることを試みます。
+もし C<dog> がマッチングしなければ、Perl は次の選択肢である C<cat> を
+試します。
+C<cat> もまたマッチングしなければ、マッチングは失敗してPerlは文字列の
+次の位置に移動します。
+幾つか例を挙げましょう:
+
+=begin original
+
+    "cats and dogs" =~ /cat|dog|bird/;  # matches "cat"
+    "cats and dogs" =~ /dog|cat|bird/;  # matches "cat"
+
+=end original
+
+    "cats and dogs" =~ /cat|dog|bird/;  # "cat" にマッチング
+    "cats and dogs" =~ /dog|cat|bird/;  # "cat" にマッチング
+
+=begin original
+
+Even though C<dog> is the first alternative in the second regex,
+C<cat> is able to match earlier in the string.
+
+=end original
+
+二番目の正規表現において最初の選択肢が C<dog> であるにもかかわらず、
+C<cat> が文字列で最初に現れるマッチング対象です。
+
+=begin original
+
+    "cats"          =~ /c|ca|cat|cats/; # matches "c"
+    "cats"          =~ /cats|cat|ca|c/; # matches "cats"
+
+=end original
+
+    "cats"          =~ /c|ca|cat|cats/; # "c" にマッチング
+    "cats"          =~ /cats|cat|ca|c/; # "cats" にマッチング
+
+=begin original
+
+At a given character position, the first alternative that allows the
+regex match to succeed will be the one that matches. Here, all the
+alternatives match at the first string position, so the first matches.
+
+=end original
+
+与えられた文字位置で、正規表現のマッチングを成功させるための
+最初の選択肢はマッチングする一つとなります。
+ここでは、全ての選択はは最初の文字列位置でマッチングするので、
+最初のものがマッチングします。
+
+=head2 Grouping things and hierarchical matching
+
+(グループ化と階層的マッチング)
+
+=begin original
+
+The B<grouping> metacharacters C<()> allow a part of a regex to be
+treated as a single unit.  Parts of a regex are grouped by enclosing
+them in parentheses.  The regex C<house(cat|keeper)> means match
+C<house> followed by either C<cat> or C<keeper>.  Some more examples
+are
+
+=end original
+
+B<グループ化> メタ文字 C<()> は正規表現の一部分を一つのユニットとして
+扱うことを許します。
+ある正規表現の一部はカッコによって囲まれることでグループ化されます。
+正規表現 C<house(cat|keeper)> は、C<cat> か C<keeper> が後続する
+C<house> にマッチングすることを意味します。
+幾つか例を挙げましょう
+
+=begin original
+
+    /(a|b)b/;    # matches 'ab' or 'bb'
+    /(^a|b)c/;   # matches 'ac' at start of string or 'bc' anywhere
+
+=end original
+
+    /(a|b)b/;    # 'ab' または 'bb' にマッチング
+    /(^a|b)c/;   # 文字列の先頭にある 'ac' か任意の場所の'bc'にマッチング
+
+=begin original
+
+    /house(cat|)/;  # matches either 'housecat' or 'house'
+    /house(cat(s|)|)/;  # matches either 'housecats' or 'housecat' or
+                        # 'house'.  Note groups can be nested.
+
+=end original
+
+    /house(cat|)/;  # 'housecat' か 'house' にマッチング
+    /house(cat(s|)|)/;  # 'housecats' か 'housecat' か 'house' のいずれかに
+                        # マッチング。グループがネストできることに注意
+
+=begin original
+
+    "20" =~ /(19|20|)\d\d/;  # matches the null alternative '()\d\d',
+                             # because '20\d\d' can't match
+
+=end original
+
+    "20" =~ /(19|20|)\d\d/;  # 空の選択肢 '()\d\d' にマッチング
+                             # '20\d\d' はマッチングできないから
+
+=head2 Extracting matches
+
+(マッチングしたものを取り出す)
+
+=begin original
+
+The grouping metacharacters C<()> also allow the extraction of the
+parts of a string that matched.  For each grouping, the part that
+matched inside goes into the special variables C<$1>, C<$2>, etc.
+They can be used just as ordinary variables:
+
+=end original
+
+グループ化メタ文字 C<()> はまた、マッチングした文字列の一部分を
+展開することができます。
+それぞれのグループ化に対して、マッチングした部分が特殊変数 C<$1>, C<$2>
+などに格納されます。
+これらの変数は通常の変数と同じように使うことができます:
+
+=begin original
+
+    # extract hours, minutes, seconds
+    $time =~ /(\d\d):(\d\d):(\d\d)/;  # match hh:mm:ss format
+    $hours = $1;
+    $minutes = $2;
+    $seconds = $3;
+
+=end original
+
+    # 時、分、秒を抽出する
+    $time =~ /(\d\d):(\d\d):(\d\d)/;  # hh:mm:ss 形式にマッチングする
+    $hours = $1;
+    $minutes = $2;
+    $seconds = $3;
+
+=begin original
+
+In list context, a match C</regex/> with groupings will return the
+list of matched values C<($1,$2,...)>.  So we could rewrite it as
+
+=end original
+
+リストコンテキストでは、グループ化付きのマッチング C</regex/> は
+マッチングした値のリスト C<($1,$2,...)> を返します。
+従ってこれは以下のように書き換えられます
+
+    ($hours, $minutes, $second) = ($time =~ /(\d\d):(\d\d):(\d\d)/);
+
+=begin original
+
+If the groupings in a regex are nested, C<$1> gets the group with the
+leftmost opening parenthesis, C<$2> the next opening parenthesis,
+etc.  For example, here is a complex regex and the matching variables
+indicated below it:
+
+=end original
+
+正規表現中のグループ化がネストしていた場合、C<$1> は最も左にある
+開きかっこによってグループ化されているものを取り、C<$2> は
+次の開きかっこによるものを取り…となっていきます。
+例えば、以下は複雑な正規表現と、後述するマッチング変数です:
+
+    /(ab(cd|ef)((gi)|j))/;
+     1  2      34
+
+=begin original
+
+Associated with the matching variables C<$1>, C<$2>, ... are
+the B<backreferences> C<\g1>, C<\g2>, ...  Backreferences are
+matching variables that can be used I<inside> a regex:
+
+=end original
+
+マッチング変数 C<$1>, C<$2> …に密接に結び付けられたものは、
+B<後方参照> (backreferences) C<\g1>, C<\g2> …です。
+後方参照は正規表現の I<内側> で使うことのできるマッチング変数です:
+
+=begin original
+
+    /(\w\w\w)\s\g1/; # find sequences like 'the the' in string
+
+=end original
+
+    /(\w\w\w)\s\g1/; # 文字列中の 'the the' のような並びを探す
+
+=begin original
+
+C<$1>, C<$2>, ... should only be used outside of a regex, and C<\g1>,
+C<\g2>, ... only inside a regex.
+
+=end original
+
+C<$1>, C<$2> …は正規表現の外側のみで用い、
+後方参照 C<\1>, C<\2> …は正規表現の内側でのみ使うようにすべきです。
+
+=head2 Matching repetitions
+
+(マッチングの繰り返し)
+
+=begin original
+
+The B<quantifier> metacharacters C<?>, C<*>, C<+>, and C<{}> allow us
+to determine the number of repeats of a portion of a regex we
+consider to be a match.  Quantifiers are put immediately after the
+character, character class, or grouping that we want to specify.  They
+have the following meanings:
+
+=end original
+
+B<量指定子> (quatifier) C<?>, C<*>, C<+>, C<{}> によって、
+マッチングさせたいと考えている正規表現の一部分の繰り返し回数を
+指定できます。
+量指定子は繰り返しを指定したい文字、文字クラス、またはグループの直後に
+置きます。
+量指定子には以下のような意味があります:
+
+=over 4
+
+=item *
+
+=begin original
+
+C<a?> = match 'a' 1 or 0 times
+
+=end original
+
+C<a?> は: 'a' または空文字列にマッチングします。
+
+=item *
+
+=begin original
+
+C<a*> = match 'a' 0 or more times, i.e., any number of times
+
+=end original
+
+C<a*> は: 'a' のゼロ回以上の繰り返しにマッチングします。
+
+=item *
+
+=begin original
+
+C<a+> = match 'a' 1 or more times, i.e., at least once
+
+=end original
+
+C<a+> は: 'a' の一回以上の繰り返しにマッチングします。
+
+=item *
+
+=begin original
+
+C<a{n,m}> = match at least C<n> times, but not more than C<m>
+times.
+
+=end original
+
+C<a{n,m}> は: C<n> 回以上 C<m> 回以下の繰り返しにマッチングします。
+
+=item *
+
+=begin original
+
+C<a{n,}> = match at least C<n> or more times
+
+=end original
+
+C<a{n,}> は: C<n> 回以上の繰り返しにマッチングします。
+
+=item *
+
+=begin original
+
+C<a{n}> = match exactly C<n> times
+
+=end original
+
+C<a{n}> は: C<n> 回の繰り返しにマッチングします。
+
+=back
+
+=begin original
+
+Here are some examples:
+
+=end original
+
+以下に幾つか例を挙げます:
+
+=begin original
+
+    /[a-z]+\s+\d*/;  # match a lowercase word, at least some space, and
+                     # any number of digits
+    /(\w+)\s+\g1/;    # match doubled words of arbitrary length
+    $year =~ /^\d{2,4}$/;  # make sure year is at least 2 but not more
+                           # than 4 digits
+    $year =~ /^\d{4}$|^\d{2}$/; # better match; throw out 3 digit dates
+
+=end original
+
+    /[a-z]+\s+\d*/;  # 小文字の単語、幾つかの空白、それに続く任意の長さの
+                     # 数字にマッチング
+    /(\w+)\s+\1/;     # 任意の長さの単語の重複にマッチング
+    $year =~ /\d{2,4}/;  # 年が少なくとも 2 桁あるが最大でも 4 桁に
+                         # なるようにする
+    $year =~ /^\d{4}$|^\d{2}$/; # もっと良い; 3桁をはじく
+
+=begin original
+
+These quantifiers will try to match as much of the string as possible,
+while still allowing the regex to match.  So we have
+
+=end original
+
+これらの量指定子はは正規表現のマッチングが成功するのを許す範囲で
+可能な限りの文字列をマッチングさせようとします。
+従って、以下のようになります
+
+=begin original
+
+    $x = 'the cat in the hat';
+    $x =~ /^(.*)(at)(.*)$/; # matches,
+                            # $1 = 'the cat in the h'
+                            # $2 = 'at'
+                            # $3 = ''   (0 matches)
+
+=end original
+
+    $x = 'the cat in the hat';
+    $x =~ /^(.*)(at)(.*)$/; # マッチングする
+                            # $1 = 'the cat in the h'
+                            # $2 = 'at'
+                            # $3 = ''   (0 回マッチング)
+
+=begin original
+
+The first quantifier C<.*> grabs as much of the string as possible
+while still having the regex match. The second quantifier C<.*> has
+no string left to it, so it matches 0 times.
+
+=end original
+
+最初の量指定子 C<.*> は正規表現がマッチングする範囲で可能な限りの
+長い文字列をつかみとります。
+2 番目の量指定子は C<.*> には文字列が残されていないので、0 回
+マッチングします。
+
+=head2 More matching
+
+=begin original
+
+There are a few more things you might want to know about matching
+operators.
+The global modifier C<//g> allows the matching operator to match
+within a string as many times as possible.  In scalar context,
+successive matches against a string will have C<//g> jump from match
+to match, keeping track of position in the string as it goes along.
+You can get or set the position with the C<pos()> function.
+For example,
+
+=end original
+
+マッチング演算子について知りたいかもしれないことがあといくつかあります。
+グローバル修飾子 C<//g> は一つの文字列に出来るだけ何回もマッチングすることを
+許します。
+スカラコンテキストでは、文字列に対するマッチングの成功によって
+C<//g> はマッチングからマッチングにジャンプし、文字列の位置を記録し続けて
+いきます。
+C<pos()> 関数を使って位置を取得または設定できます。
+例えば:
+
+    $x = "cat dog house"; # 3 words
+    while ($x =~ /(\w+)/g) {
+        print "Word is $1, ends at position ", pos $x, "\n";
+    }
+
+=begin original
+
+prints
+
+=end original
+
+は以下を表示します
+
+    Word is cat, ends at position 3
+    Word is dog, ends at position 7
+    Word is house, ends at position 13
+
+=begin original
+
+A failed match or changing the target string resets the position.  If
+you don't want the position reset after failure to match, add the
+C<//c>, as in C</regex/gc>.
+
+=end original
+
+マッチングに失敗したり、ターゲット文字列を変更するとこの位置は
+リセットされます。
+もしマッチングに失敗したときに位置をリセットしたくないのであれば、
+C</regexp/gc> のように C<//c> を追加します。
+
+=begin original
+
+In list context, C<//g> returns a list of matched groupings, or if
+there are no groupings, a list of matches to the whole regex.  So
+
+=end original
+
+リストコンテキストでは、C<//g> はマッチングしたグループのリストを返します;
+グループ化の指定がなければ、正規表現全体にマッチングするリストを返します。
+従って
+
+=begin original
+
+    @words = ($x =~ /(\w+)/g);  # matches,
+                                # $word[0] = 'cat'
+                                # $word[1] = 'dog'
+                                # $word[2] = 'house'
+
+=end original
+
+    @words = ($x =~ /(\w+)/g);  # マッチングする
+                                # $word[0] = 'cat'
+                                # $word[1] = 'dog'
+                                # $word[2] = 'house'
+
+=head2 Search and replace
+
+(検索と置換)
+
+=begin original
+
+Search and replace is performed using C<s/regex/replacement/modifiers>.
+The C<replacement> is a Perl double-quoted string that replaces in the
+string whatever is matched with the C<regex>.  The operator C<=~> is
+also used here to associate a string with C<s///>.  If matching
+against C<$_>, the S<C<$_ =~>> can be dropped.  If there is a match,
+C<s///> returns the number of substitutions made; otherwise it returns
+false.  Here are a few examples:
+
+=end original
+
+検索と置換は C<s/regexp/replacement/modifiers> を使って処理されます。
+C<replacement> は Perlでのダブルクォートで囲まれた文字列で、
+C<regexp> にマッチングした文字列を置き換えるものです。
+C<=~> 演算子もまた C<s///> を伴った文字列に結びつけられるために
+使われます。
+C<$_> に対してマッチングを行う場合には、S<C<$_ =~> > は省略できます。
+マッチングに成功した場合には C<s///> は置換が行われた数を返します;
+失敗した場合には偽を返します。
+幾つか例を挙げましょう:
+
+=begin original
+
+    $x = "Time to feed the cat!";
+    $x =~ s/cat/hacker/;   # $x contains "Time to feed the hacker!"
+    $y = "'quoted words'";
+    $y =~ s/^'(.*)'$/$1/;  # strip single quotes,
+                           # $y contains "quoted words"
+
+=end original
+
+    $x = "Time to feed the cat!";
+    $x =~ s/cat/hacker/;   # $x の内容は "Time to feed the hacker!"
+    $y = "'quoted words'";
+    $y =~ s/^'(.*)'$/$1/;  # シングルクォートを剥ぎ取る
+                           # $y の内容は "quoted words"
+
+=begin original
+
+With the C<s///> operator, the matched variables C<$1>, C<$2>, etc.
+are immediately available for use in the replacement expression. With
+the global modifier, C<s///g> will search and replace all occurrences
+of the regex in the string:
+
+=end original
+
+C<s///> 演算子を使うにあたって、C<$1>, C<$2> といったマッチング変数は
+その置換式のなかで即座に使うことができます。
+グローバル修飾子 C<s///g> を使うことで、文字列中のすべての正規表現に
+マッチングする検索と置換を行います:
+
+=begin original
+
+    $x = "I batted 4 for 4";
+    $x =~ s/4/four/;   # $x contains "I batted four for 4"
+    $x = "I batted 4 for 4";
+    $x =~ s/4/four/g;  # $x contains "I batted four for four"
+
+=end original
+
+    $x = "I batted 4 for 4";
+    $x =~ s/4/four/;   # $x の内容は "I batted four for 4"
+    $x = "I batted 4 for 4";
+    $x =~ s/4/four/g;  # $x の内容は "I batted four for four"
+
+=begin original
+
+The non-destructive modifier C<s///r> causes the result of the substitution
+to be returned instead of modifying C<$_> (or whatever variable the
+substitute was bound to with C<=~>):
+
+=end original
+
+非破壊修飾子 C<s///r> は C<$_> (または C<=~> によって置換されることになる
+変数) を変更する代わりに、置換の結果を返します:
+
+    $x = "I like dogs.";
+    $y = $x =~ s/dogs/cats/r;
+    print "$x $y\n"; # prints "I like dogs. I like cats."
+
+    $x = "Cats are great.";
+    print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~
+        s/Frogs/Hedgehogs/r, "\n";
+    # prints "Hedgehogs are great."
+
+    @foo = map { s/[a-z]/X/r } qw(a b c 1 2 3);
+    # @foo is now qw(X X X 1 2 3)
+
+=begin original
+
+The evaluation modifier C<s///e> wraps an C<eval{...}> around the
+replacement string and the evaluated result is substituted for the
+matched substring.  Some examples:
+
+=end original
+
+評価修飾子 C<s///e> は置換文字列を C<eval{...}> でラップし、その評価結果を
+マッチングした部分文字列の置換のために使います。
+いくつか例を挙げます:
+
+=begin original
+
+    # reverse all the words in a string
+    $x = "the cat in the hat";
+    $x =~ s/(\w+)/reverse $1/ge;   # $x contains "eht tac ni eht tah"
+
+=end original
+
+    # 文字列中の全ての単語を逆順にする
+    $x = "the cat in the hat";
+    $x =~ s/(\w+)/reverse $1/ge;   # $x は "eht tac ni eht tah"
+
+=begin original
+
+    # convert percentage to decimal
+    $x = "A 39% hit rate";
+    $x =~ s!(\d+)%!$1/100!e;       # $x contains "A 0.39 hit rate"
+
+=end original
+
+    # 百分率を 10 進数に置き換える
+    $x = "A 39% hit rate";
+    $x =~ s!(\d+)%!$1/100!e;       # $x は "A 0.39 hit rate"
+
+=begin original
+
+The last example shows that C<s///> can use other delimiters, such as
+C<s!!!> and C<s{}{}>, and even C<s{}//>.  If single quotes are used
+C<s'''>, then the regex and replacement are treated as single-quoted
+strings.
+
+=end original
+
+最後の例のように、C<s///> も C<s!!!> や C<s{}{}> 、
+果ては C<s{}//> のように異なるデリミタを使うことができます。
+C<s'''> のようにシングルクォートが使われた場合、その正規表現と
+置換テキストはシングルクォート文字列のように扱われ、変数の置き換えは
+行われません。
+
+=head2 The split operator
+
+(split 演算子)
+
+=begin original
+
+C<split /regex/, string> splits C<string> into a list of substrings
+and returns that list.  The regex determines the character sequence
+that C<string> is split with respect to.  For example, to split a
+string into words, use
+
+=end original
+
+C<split /regex/, string, limit> は C<string> オペランドを部分文字列の
+リストに分割し、そのリストを返します。
+regex は、C<string> を分割するときに使われる文字並びを決定します。
+たとえば、文字列を単語に分割するには以下のようにします
+
+    $x = "Calvin and Hobbes";
+    @word = split /\s+/, $x;  # $word[0] = 'Calvin'
+                              # $word[1] = 'and'
+                              # $word[2] = 'Hobbes'
+
+=begin original
+
+To extract a comma-delimited list of numbers, use
+
+=end original
+
+カンマ区切りの数値リストを展開するには、以下のようにします
+
+    $x = "1.618,2.718,   3.142";
+    @const = split /,\s*/, $x;  # $const[0] = '1.618'
+                                # $const[1] = '2.718'
+                                # $const[2] = '3.142'
+
+=begin original
+
+If the empty regex C<//> is used, the string is split into individual
+characters.  If the regex has groupings, then the list produced contains
+the matched substrings from the groupings as well:
+
+=end original
+
+C<//> が使われた場合には、文字列は個々の文字に分割されます。
+正規表現がグループ化を伴っていた場合には、グループ化されたものも部分文字列に
+含まれるようになります:
+
+    $x = "/usr/bin";
+    @parts = split m!(/)!, $x;  # $parts[0] = ''
+                                # $parts[1] = '/'
+                                # $parts[2] = 'usr'
+                                # $parts[3] = '/'
+                                # $parts[4] = 'bin'
+
+=begin original
+
+Since the first character of $x matched the regex, C<split> prepended
+an empty initial element to the list.
+
+=end original
+
+$x の最初の文字に正規表現がマッチングしているので、C<split> はリストの
+最初の要素に空要素を置きます。
+
+=head2 C<use re 'strict'>
+
+=begin original
+
+New in v5.22, this applies stricter rules than otherwise when compiling
+regular expression patterns.  It can find things that, while legal, may
+not be what you intended.
+
+=end original
+
+これは v5.22 からの新機能で、正規表現パターンをコンパイルするときに他よりも
+より厳密な規則を適用します。
+これにより、有効ではあるけれども、意図しているものと違うかも知れないものを
+見つけることができます。
+
+=begin original
+
+See L<'strict' in re|re/'strict' mode>.
+
+=end original
+
+L<'strict' in re|re/'strict' mode> を参照してください。
+
+=head1 BUGS
+
+=begin original
+
+None.
+
+=end original
+
+なし。
+
+=head1 SEE ALSO
+
+=begin original
+
+This is just a quick start guide.  For a more in-depth tutorial on
+regexes, see L<perlretut> and for the reference page, see L<perlre>.
+
+=end original
+
+これは単なるクイックスタートガイドです。
+正規表現に関するより深いチュートリアルについては L<perlretut> を、
+リファレンスについては L<perlre> を参照してください。
+
+=head1 AUTHOR AND COPYRIGHT
+
+Copyright (c) 2000 Mark Kvale
+All rights reserved.
+
+This document may be distributed under the same terms as Perl itself.
+
+=head2 Acknowledgments
+
+The author would like to thank Mark-Jason Dominus, Tom Christiansen,
+Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpful
+comments.
+
+=cut
+
+
+=begin meta
+
+Translate: SHIRAKATA Kentaro <argra****@ub32*****>
+Status: completed
+
+=end meta
+



perldocjp-cvs メーリングリストの案内
Back to archive index