正则表达式在第一个正斜杠之前排除基于字符串的匹配

时间:2013-11-28 12:08:32

标签: regex

我想要任何导致扩展名为“.log”的文件的路径字符串,或者包含要从匹配中排除的目录“tmp”的路径

我快到了:

(?!tmp).+?\.(?!log|tmp).+

http://rubular.com/r/Ubkz7MIEGH

我想要的是

TMP / hello.jpg

以与

相同的方式排除
hello.log
hmm.tmp

被排除在外。

3 个答案:

答案 0 :(得分:0)

试试以下正则表达式:

^(?!(?:.*log$)|tmp).*$

答案 1 :(得分:0)

怎么样:

^(?!.*\btmp\b)(?!.+\.log\b)(.+)$

<强>解释

The regular expression:

(?-imsx:^(?!.*\btmp\b)(?!.+\.log\b)(.+)$)

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
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
    tmp                      'tmp'
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    log                      'log'
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

答案 2 :(得分:0)

^(?!tmp).*(?<!\.tmp|log)$

这只是一个负面的背后隐藏。 Live demo