私はこれを考え出すタフな時間を過ごしています:この畳み込み正規表現は何をしていますか?
($dwg, $rev, $rest) = ($file =~ /^(\d{3}[_-][\w\d]{3}[_-]\d{3,4}(?:[_-]\d{3,4})?)(?:[_ -]\w)?[_ ]{0,5}[rR](?:[eE][vV])?(?:\.)? ?([\w\d-]?) *(.*)/);
私はこれを考え出すタフな時間を過ごしています:この畳み込み正規表現は何をしていますか?
($dwg, $rev, $rest) = ($file =~ /^(\d{3}[_-][\w\d]{3}[_-]\d{3,4}(?:[_-]\d{3,4})?)(?:[_ -]\w)?[_ ]{0,5}[rR](?:[eE][vV])?(?:\.)? ?([\w\d-]?) *(.*)/);
それは$dwg
、$rev
と$rest
に$file
から3つのcatchedグループを置くだけで、複雑な正規表現、です。
正規表現は複雑ですが、非常に複雑なルールは使用しません。おそらく(?:something)
(これはキャッチしていないグループを除く)です。
たとえば、perl正規表現の紹介としてthisを参照してください。
日付が$dwg
、改訂版$rev
、末尾に$rest
というファイル名が抽出されているようです。概して、日付はアンダースコアまたはハイフンで区切られた最大4つのセクションを持つことができ、リビジョンはrev
(大文字または小文字)で始まる一連の単語文字であり、接尾辞は改訂後の最初の空白に続くすべての文字を含みます。それはかなり面倒です、そして、それはすぐに多くの微妙に異なるケースを説明しようとしているようです。ここで
^ # After the start of the string,
( # $dwg gets
\d{3} # three digits,
[_-] # a separator,
[\w\d]{3} # three word characters,
[_-] # another separator,
\d{3,4} # three or four digits,
(?: # and
[_-] # a separator and
\d{3,4} # three or four more digits
)? # which are optional.
)
(?: # Next,
[_ -] # another separator,
\w # followed by a word character,
)? # also optional;
[_ ]{0,5} # a separator up to five characters long,
[rR] # then "R" or "r",
(?:
[eE] # or "rev" in any mix of case,
[vV]
)? # optionally;
(?:
\. # a dot,
)? # which too is optional;
? # and an optional space.
( # $rev gets
[\w\d-]? # an optional word character or dash.
)
* # Any number of spaces later,
(.*) # $rest gets the rest.
Ha。私はすべてを通過するにはあまりにも怠惰だった:) –
はい、私たちはほぼ同じ説明を持っています。ちょうどポイント: '{3}'は '正確に3回の出現 'を意味し、3回までは意味しません。 – Toto
@ M42:そうです。私はその日の時間を責める。一定。 –
は説明です:
^ : begining of string
( : start group 1; it populates $dwg
\d{3} : 3 digit
[_-] : _ or - character
[\w\d]{3} : 3 alphanum, could be abreviated as \w{3}
[_-] : _ or - character
\d{3,4} : 3 or 4 digit
(?: : start NON capture group
[_-] : _ or - character
\d{3,4} : 3 or 4 digit
)? : end of non capture group optionnal
) : end of group 1
(?: : start NON capture group
[_ -] : _ or space or - character
\w : 1 alphanum
)? : end of non capture group optionnal
[_ ]{0,5} : 0 to 5 _ or space char
[rR] : r or R
(?: : start NON capture group
[eE] : e or E
[vV] : v or V
)? : end of non capture group optionnal
(?:\.)? : a dot not captured optionnal
? : optionnal space
([\w\d-]?) : group 2, 1 aphanum or - could be [\w-]; populates $rev
* : 0 or more spaces
(.*) : any number of any char but linefeed; populates $rest
+1ダング、私は私の編集する前にあなたの答えを見ていない。しかたがない。私たちはそれについて別のことをすると思います。 –
YAPE::Regex::Explainは、入力として任意の正規表現を受け入れモジュールであり、出力として正規表現が何をするかの説明を提供しています。ここでは例です:
use Modern::Perl;
use YAPE::Regex::Explain;
my $re = qr/^(\d{3}[_-][\w\d]{3}[_-]\d{3,4}(?:[_-]\d{3,4})?)(?:[_ -]\w)?[_ ]{0,5}[rR](?:[eE][vV])?(?:\.)? ?([\w\d-]?) *(.*)/;
say YAPE::Regex::Explain->new($re)->explain();
そして、ここでは出力です:多くの場合、それが簡単に外部のツールに頼ることなく、正規表現を解読することができ
The regular expression:
(?-imsx:^(\d{3}[_-][\w\d]{3}[_-]\d{3,4}(?:[_-]\d{3,4})?)(?:[_ -]\w)?[_ ]{0,5}[rR](?:[eE][vV])?(?:\.)? ?([\w\d-]?) *(.*))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with^and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
\d{3} digits (0-9) (3 times)
----------------------------------------------------------------------
[_-] any character of: '_', '-'
----------------------------------------------------------------------
[\w\d]{3} any character of: word characters (a-z,
A-Z, 0-9, _), digits (0-9) (3 times)
----------------------------------------------------------------------
[_-] any character of: '_', '-'
----------------------------------------------------------------------
\d{3,4} digits (0-9) (between 3 and 4 times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[_-] any character of: '_', '-'
----------------------------------------------------------------------
\d{3,4} digits (0-9) (between 3 and 4 times
(matching the most amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[_ -] any character of: '_', ' ', '-'
----------------------------------------------------------------------
\w word characters (a-z, A-Z, 0-9, _)
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
[_ ]{0,5} any character of: '_', ' ' (between 0 and
5 times (matching the most amount
possible))
----------------------------------------------------------------------
[rR] any character of: 'r', 'R'
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[eE] any character of: 'e', 'E'
----------------------------------------------------------------------
[vV] any character of: 'v', 'V'
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
? ' ' (optional (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[\w\d-]? any character of: word characters (a-z,
A-Z, 0-9, _), digits (0-9), '-'
(optional (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
* ' ' (0 or more times (matching the most
amount possible))
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
ことの一つは、最後に/ x修飾子を置くことです正規表現の中で自由形式の空白がほとんど許されます。/x修飾子を使うと、式の関数を変更することなく改行やタブを含む空白を正規表現に挿入することができます。これは正規表現の部分をグループ化することを容易にします。もちろん、REに重要な空白が埋め込まれていれば、これはうまくいきません。そのような場合、あなたは表現の意味を変えてしまいます。しかし、通常の正規表現では/ x修飾子は意味のクラスタに分解するための第一歩です。例えば、私はこのようなあなたの正規表現に
を始めるかもしれません。私にとって
m/^
(
\d{3} [_-] [\w\d]{3} [_-] \d{3,4}
(?:
[_-] \d{3,4}
)?
)
# ......and so on.
/x
、これを実行すると、私はより良い何が起こっているのか視覚化するのに役立ちます。 正規表現は、perlrequick(クイックスタートガイド)、perlretut(詳細なチュートリアル)、perlre(最終的なソース)、perlopのPODで読むことができます。しかし、Jeffrey Friedlの傑作「Mastering Regular Expressions」(O'Reilly - Curently第3版)と同じように役立つものは何もありません。
注:このREには、最後に1つの埋め込みスペースがあるようです。それは\ x20と表記されている方が目立ちやすく、そのように変更すると/ x修飾子を使用するのが安全になります。
+1 YAPE :: Regex :: Explain –
エディタで角括弧を使用してコードをフォーマットしてください –
expresso(http://www.ultrapico.com/Expresso.htm)のような正規表現ツールに入れてみましたか? – Rohith
このコード行は、正規表現(またはより一般的でおそらくより正確には、正規表現)と呼ばれます。それはあなたにGoogleの助けになるはずですが、私はあなたがここで多くの助けを見つけると確信しています。 –