一致するように正規表現を定義したフォローがありhttp urls
ますが、英語で説明するのを手伝ってくれる人はいますか?
^/foo/.*(?<!\.css|\.js|\.jpg)$
、またはファイルではない、ディレクトリ/foo
以下のファイルまたはディレクトリ。css
js
jpg
^ start of string anchor
/foo/ literal "/foo/"
.* any number of characters
(?<!...) match from here backwards must fail:
\. dot
css literal "css"
| or
$ end of string anchor
したがって、文字列の開始/foo/
、、おそらく他のいくつかの文字、そして文字列の終了-しかし、直前に、、またはをすることはでき.css
ませ.js
ん.jpg
。
編集:頑固であることをお詫びします。これは、Perlを含むほとんどのエンジンにとって実際に無効な正規表現です。その理由は、ネガティブルックビハインドは固定幅でなければならないからです。この後読みは、4文字(またはの.jpg
場合.css
)または3文字(.js
)のいずれかになります。修正は、幅が常に4になるように、後読みに追加の「何でも一致」を挿入することです。
^/foo/.*(?<!\.css|.\.js|\.jpg)$
それで、それは機能します:
perl -e 'print "/foo/bar" =~ m[^/foo/.*(?<!\.css|.\.js|\.jpg)$];'
=> 1
OP:regexpal.comの問題は、JavaScriptの正規表現をテストしているという事実です。これは、ネガティブな後読みをまったく実装していません。正規表現の方言は詳細が異なります。
あなたのパターンは、以下と同じように一致するように意図されています:
m{^/foo/} && !m{\.(?:css|js|jpg)\z}
これは非常に読みやすいので、説明する必要はありません。また、パターンとは異なり、実際にコンパイルするという利点もあります。
>perl -c -e"m{^/foo/.*(?<!\.css|\.js|\.jpg)$}"
Variable length lookbehind not implemented in regex m/^/foo/.*(?<!\.css|\.js|\.jpg)$/ at -e line 1.
どんな文字列にも一致します
/foo/
.css
、.js
またはのいずれかで終わらない.jpg
foo
したがって、CSS、JS、またはJPGではないディレクトリ(直接またはサブディレクトリとして)をルートとするすべてのファイルと一致します。
を使用して正規表現を学習しYAPE::Regex::Explain
ます。以下のコードを確認してください。
#!/usr/bin/perl -w
use strict;
use YAPE::Regex::Explain;
my $regex = '^/foo/.*(?<!\.css|\.js|\.jpg)$';
print YAPE::Regex::Explain->new($regex)->explain();
出力:
The regular expression:
(?-imsx:^/foo/.*(?<!\.css|\.js|\.jpg)$)
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
----------------------------------------------------------------------
/foo/ '/foo/'
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?<! look behind to see if there is not:
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
css 'css'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
js 'js'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
jpg 'jpg'
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------