0

一致するように正規表現を定義したフォローがありhttp urlsますが、英語で説明するのを手伝ってくれる人はいますか?

^/foo/.*(?<!\.css|\.js|\.jpg)$
4

4 に答える 4

6

、またはファイルではない、ディレクトリ/foo以下のファイルまたはディレクトリ。cssjsjpg

^        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の正規表現をテストしているという事実です。これは、ネガティブな後読みをまったく実装していません。正規表現の方言は詳細が異なります。

于 2013-01-12T06:08:43.133 に答える
4

あなたのパターンは、以下と同じように一致するように意図されています:

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.
于 2013-01-12T06:19:38.050 に答える
3

どんな文字列にも一致します

  1. で始まる/foo/
  2. 何でも続く
  3. .css.jsまたはのいずれかで終わらない.jpg

fooしたがって、CSS、JS、またはJPGではないディレクトリ(直接またはサブディレクトリとして)をルートとするすべてのファイルと一致します。

于 2013-01-12T06:09:01.497 に答える
2

を使用して正規表現を学習し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
----------------------------------------------------------------------
于 2013-01-12T06:33:53.750 に答える