正規表現に関してプログラマーが覚えておくべき最低限のコト - The absolute bare minimum every programmer should know about regular expressions

The absolute bare minimum every programmer should know about regular expressionsの日本語訳。正規表現まとめサイトで紹介されていました。
正規表現の大事な点が簡潔に説明されているので、正規表現の入門に良いと思います。

正規表現って何? - Wtf is a regular expression?

正規表現とは、テキストの表現や解析を行うために、特別なパターン表記法を使って書いた文字列です。多くのプログラマーは(一部の優秀なプログラマーでさえ)、正規表現を不愉快なモノと考えて無視しています。正規表現が役に立つ場面はとても多いので、これは非常に残念なことです。一度、正規表現の扱い方を覚えてしまえば、現実世界の無数の問題の解決に使えます。
正規表現はファイル名「グロブ」のように動作します。Windowsや*nixでは、特殊文字*と?を使用したグロブによって、複数のファイルを指定できます(おっと!グロブを使ってグロブを定義してしまった?)。しかし、正規表現特殊文字とメタ文字にはそれ以上の表現力があります。
グロブと同様に正規表現でも、文字のほとんどは普通のテキストとして扱われます。例えば、正規表現mikeは、m - i - k - e の順に並んだ文字列にマッチします。ですが、正規表現の豊富なメタキャラクタを見れば、普通のグロブがとても原始的に見えます。

Regular expressions are strings formatted using a special pattern notation that allow you to describe and parse text. Many programmers (even some good ones) disregard regular expressions as line noise, and it’s a damned shame because they come in handy so often. Once you’ve gotten the hang of them, regular expressions can be used to solve countless real world problems.
Regular expressions work a lot like the filename “globs” in Windows or *nix that allow you to specify a number of files by using the special * or ? characters (oops, did I just use a glob while defining a glob?). But with regular expressions the special characters, or metacharacters, are far more expressive.
Like globs, regular expressions treat most characters as literal text. The regular expression mike, for example, will only match the letters m - i - k - e, in that order. But regular expressions sport an extensive set of metacharacters that make the simple glob look downright primitive.

メタ文字を知る: 「^(){}.*?\|+$ と 時々-」 - Meet the metacharacters: ^(){}.*?\|+$ and sometimes -

メタ文字は恐ろしいモノに見えるかもしれませんが、一度わかってしまえばばとてもよいモノです。

I know, they look intimidating, but they’re really nice characters once you get to know them.

行アンカー: 「^」と「$」 - The Line Anchors: ‘^’ and ‘$’

メタ文字「^」(キャレット)と「$」(ドル)は、それぞれテキスト行の先頭と末尾を表します。先に述べたとおり、正規表現mikeはm - i - k - eという文字列にマッチしますが、行内のどこにこの文字列があってもマッチします(例えば、「I'm mike」にマッチしますし、「carmike」にもマッチします)。文字「^」を使うと、マッチする箇所が行の先頭に固定されます。したがって、^mikeは、mikeから始まる行を見つけます。同様に、正規表現mike$は、行の末尾のm - i - k - eにだけを見つけます(この正規表現も「carmike」にマッチします)。
これら二つの行アンカー文字を組み合わせると、特定の文字列に一致するテキスト行を探せます。正規表現^mike$は、単語mikeに一致する行にマッチします。同様に、正規表現^$を使えば、空の行を見つけられます。空の行では、行の先頭のすぐ後が行の末尾です。

The ‘^’ (caret) and ‘$’ (dollar) metacharacters represent the start and end of a line of text, respectively. As I mentioned earlier, the regular expression mike will match the letters m - i - k - e, but it will match anywhere in a line (e.g. it would match “I’m mike”, or even “carmike”). The ‘^’ character is used to anchor the match to the start of the line, so ^mike would only find lines that start with mike. Similarly, the expression mike$ would only find m - i - k - e at the end of a line (but would still match ‘carmike’).
If we combine the two line anchor characters we can search for lines of text that contain a specific sequence of characters. The expression ^mike$ will only match the word mike on a line by itself - nothing more, nothing less. Similarly the expression ^$ is useful for finding empty lines, where the beginning of the line is promptly followed by the end.

文字クラス: 「」 - The Character Class: ‘

角括弧(文字クラス)を使うと、複数の文字のうちいずれか一つにマッチさせることができます。単語「gray」にマッチさせたくて、さらに「grey」と綴ったものも見つけたいとします。文字クラスを使えば両方をマッチさせることができます。正規表現gr[ea]yは、「g、その次にr、その次にeまたはa、その次にy」と解釈されます。
[ ... ]の代わりに[^ ... ]を使った文字クラスは、書かれていない任意の文字にマッチします。先頭の^が書かれた文字を「否定」します。必要な文字をクラス内にすべて書く代わりに、不要な文字を書きます。なお、文字クラス外の^とは違い、文字クラス内の^ (キャレット)には行の先頭へのマッチの意味はありません。

Square brackets, called a character class, let you match any one of several characters. Suppose you want to match the word ‘gray’, but also want to find it if it was spelled ‘grey’. A character class will allow you to match either. The regular expression gr[ea]y is interpreted as “g, followed by r, followed by either an e or an a, followed by y.”
If you use [^ ... ] instead of [ ... ], the class matches any character that isn’t listed. The leading ^ “negates” the list. Instead of listing all of the characters you want to included in the class, you list the characters you don’t want included. Note that the ^ (caret) character used here has a different meaning when it’s used outside of a character class, where it is used to match the beginning of a line.

文字クラスメタ文字: 「-」 - The Character Class Metacharacter: ‘-’

文字クラス内にある文字クラスメタ文字「-」 (ダッシュ)は、文字の範囲を表します。[01234567890abcdefABCDEF]と書く代わりに、[0-9a-fA-F]と書けます。とても便利ですね!ダッシュ文字がメタ文字になるのは文字クラス内だけで、文字クラス外では単純にダッシュにマッチします。
ところで、これには落とし穴があります。文字クラスの最初に書いたダッシュは、メタ文字にはならず(範囲には先頭と末尾が必要なので、このダッシュが範囲を表すことはありません)、通常のダッシュにマッチします。同様に、疑問符やピリオドは正規表現のメタ文字ですが、文字クラス内ではメタ文字になりません(文字クラス[-0-9.?]に含まれる特殊文字は、0と9に挟まれたダッシュだけです)。

Within a character-class, the character-class metacharacter ‘-’ (dash) indicates a range of characters. Instead of [01234567890abcdefABCDEF] we can write [0-9a-fA-F]. How convenient. The dash is a metacharacter only within a character class, elsewhere it simply matches the normal dash character.
But wait, there’s a catch. If a dash is the first character in a character class it is not considered a metacharacter (it can’t possibly represent a range, since a range requires a beginning and an end), and will match a normal dash character. Similarly, the question mark and period are usually regex metacharacters, but not when they’re inside a class (in the class [-0-9.?] the only special character is the dash between the 0 and 9).

すべての文字へのマッチ: 「.」 - Matching Any Character With a Dot: ‘.’

「.」メタキャラクタ(ドットまたは点と呼ばれる)は、すべての文字にマッチする文字クラスの簡略記法です。文字列中で任意の文字をマッチさせたい場合に、とても便利です。繰り返しになりますが、文字クラス内では、ドットメタ文字はメタ文字になりません。

The ‘.’ metacharacter (called a dot or point) is shorthand for a character class that matches any character. It’s very convenient when you want to match any character at a particular position in a string. Once again, the dot metacharacter is not a metacharacter when it’s inside of a character class. Are you beginning to see a pattern? The list of metacharacters is different inside and outside of a character class.

選択メタキャラクタ - The Alternation Metacharacter: ‘|’

「|」メタキャラクタ(パイプ)は、「または」を表します。これを使って複数の正規表現を組み合わせると、それぞれ正規表現のうちいずれか一つにマッチする単一の正規表現を作れます。組み合わされた個々の正規表現を、選択肢と呼びます。
例えば、MikeとMichaelは別個の正規表現ですが、Mike|Michaelは両方にマッチする単一の正規表現です。
かっこを使えば、選択肢のスコープを限定できます。MikeとMickaelにマッチする上記の正規表現は、かっこを使って短くできます。正規表現Mi(ke|chael)は、同じものにマッチします。とはいえ、最初の正規表現の方が読みやすくてメンテナンス性が高いので、実際には最初のものを使うと思います。

The ‘|’ metacharacter, (pipe) means “or.” It allows you to combine multiple expressions into a single expression that matches any of the individual ones. The subexpressions are called alternatives.
For example, Mike and Michael are separate expressions, but Mike|Michael is one expression that matches either.
Parenthesis can be used to limit the scope of the alternatives. I could shorten our previous expression that matched Mike or Michael with creative use of parenthesis. The expression Mi(ke|chael) matches the same thing. I probably would use the first expression in practice, however, as it is more legible and therefore more maintainable.

任意の項目へのマッチ - Matching Optional Items: ‘?’

「?」メタ文字 (疑問符)は、任意を表します。これは、表現中に出現してもよいけれど、必ずしも出現しなくてもよい文字の後ろに置きます。この疑問符は直前の文字だけに係ります。
単語「flavor」のイギリス版とアメリカ版の両方をマッチさせる場合、正規表現flavou?rを使います。これは「f、その次にl、その次にa、その次にv、その次にo、その次に任意でu、その次にr」と解釈されます。

The ‘?’ metacharacter (question mark) means optional. It is placed after a character that is allowed, but not required, at a certain point in an expression. The question mark attaches only to the immediately preceding character.
If I wanted to match the english or american versions of the word ‘flavor’ I could use the regex flavou?r, which is interpreted as “f, followed by l, followed by a, followed by v, followed by o, followed by an optional u, followed by r.”

その他の量指定子: 「+」と「*」 - The Other Quantifiers: ‘+’ and ‘*’

疑問符のように、メタ文字「+」 (プラス)と「*」 (アスタリスク)は、直前の文字が表現中に出現する回数に影響します(「?」を使うと、直前の文字が0または1回出現したときにマッチします)。メタ文字「+」を使うと、直前の文字が1回以上出現したときにマッチし、「*」を使うと、直前の文字が0回以上出現したときにマッチします。
サッカーのスコアを決めるのために、アナウンサーが発した単語「goal」が記録されたを数える場合には、正規表現go+alが使えます。これは、「goal」にも「gooooooooooooooooal」にもマッチします(その一方、「gal」にはマッチしません)。
疑問符とプラス、アスタリスクの三つのメタ文字は、量指定子と呼ばれます。これらのメタ文字が係る項目の量に影響するからです。

Like the question mark, the ‘+’ (plus) and ‘*’ (star) metacharacters affect the number of times the preceding character can appear in the expression (with ‘?’ the preceding character could appear 0 or 1 times). The metacharacter ‘+’ matches one or more of the immediately preceding item, while ‘*’ matches any number of the preceding item, including 0.
If I was trying to determine the score of a soccer match by counting the number of times the announcer said the word ‘goal’ in a transcript, I might use the regular expression go+al, which would match ‘goal’, as well as ‘gooooooooooooooooal’ (but not ‘gal’).
The three metacharacters, question mark, plus, and star are called quantifiers because they influence the quantity of the item they’re attached to.

区間量指定子: 「{}」 - The Interval Quantifier: ‘{}’

「{最小, 最大}」メタ文字列を使うと、直前の項目が特定の回数出現した場合にマッチさせることができます。出現回数の最小と最大は自分で指定できます。正規表現go{1,5}alは、先ほどの例を制限したもので、oが1個以上5個以下のときにマッチします。{0,1}は、疑問符とまったく同じです。

The ‘{min, max}’ metasequence allows you to specify the number of times a particular item can match by providing your own minimum and maximum. The regex go{1,5}al would limit our previous example to matching between one and five o’s. The sequence {0,1} is identical to a question mark.

エスケープ文字: 「\」 - The Escape Character: ‘\’

「\」メタ文字(バックスラッシュ)は、特殊な意味を持つメタ文字をエスケープして、文字自体をマッチさせるために使います。例えば、文字「?」や「\」をマッチさせたい場合、これらの文字の直前にバックスラッシュを置いて特殊な意味を取り除きます。つまり、「\?」や「\\」とします。
メタ文字でない文字の直前にバックスラッシュを置いた時の動作は、使用している正規表現の種類によって異なります。perl互換の正規表現(PCREs)の場合、perldocのperl正規表現のページで確認してください。PCREsはとても一般的であり、この種類の正規表現PHPRubyECMAScript/Javascript、さらに、その他の言語でも使われています。

The ‘\’ metacharacter (backslash) is used to escape metacharacters that have special meaning so you can match them in patterns. For example, if you would like to match the ‘?’ or ‘\’ characters, you can precede them with a backslash, which removes their meaning: ‘\?’ or ‘\\’.
When used before a non-metacharacter a backslash can have a different meaning depending on the flavor of regular expression you’re using. For perl compatible regular expressions (PCREs) you can check out the perldoc page for perl regular expressions. PCREs are extremely common, this flavor of regexes can be used in PHP, Ruby, and ECMAScript/Javascript, and many other languages.

括弧を使用したマッチング: 「()」 - Using Parenthesis for Matching: ‘()’

多くの正規表現ツールでは、かっこを使って表現の一部を取得できます。http://([^/]+)のような正規表現を使って、URLのドメイン部分をマッチさせることができます。この正規表現を部分部分に分けて、動きを確認します。
この正規表現の始まりはとても単純で、文字列「h - t - t - p - : - / - /」にマッチします。この始めの文字列の後にかっこが続きます。かっこで囲まれた部分正規表現にマッチする文字列を取得できます。この場合の部分正規表現は「[^/]+」で、/以外の文字が1個以上続く場合にマッチします。http://immike.net/blog/Some-blog-postのようなURLでは、このかっこにより「immike.net」を取得できます。

Most regular expression tools will allow you to capture a particular subset of an expression with parenthesis. I could match the domain portion of a URL by using an expression like http://([^/]+). Let’s break this expression down into it’s components to see how it works.
The beginning of the expression is fairly straightforward: it matches the sequence “h - t - t - p - : - / - /”. This initial sequence is followed by parenthesis, which are used to capture the characters that match the subexpression they surround. In this case the subexpression is ‘[^/]+’, which matches any character except for ‘/’ one or more times. For a URL like http://immike.net/blog/Some-blog-post, ‘immike.net’ will be captured by the parenthesis.

もっと知りたい? - Want to know more?

この記事では、正規表現ができることの表面にしか触れませんでした。もっと学びたい場合は、Jeffrey Friedlの書籍である詳説正規表現を読んでください。Friedlはこの本で素晴らしい仕事をしています。この本は飾り気のないタイトルの割に、わかりやすく、驚くほど面白くて興味深い読み物になっています。
この記事の続編も見てください。一般的なプログラミング作業で役立つ正規表現のうちのいくつかについて考えています。そして、基本を理解したら、正規表現のプロになるのに必要なことを学ぶを読んでください。

I’ve only touched the surface on what can be done with regular expressions. If want to learn more, check out Jeffrey Friedl’s book Mastering Regular Expressions. Friedl did an outstanding job with this book, it’s accessible and a surprisingly fun and interesting read, considering the dry subject matter.
Check out my follow up to this article where I take a look at some of the most useful regular expressions for common programming tasks. And once you understand the basics read on to learn all you need to know to become a regex pro.