2014-02-24 69 views
2

我想忽略某個文件夾,但保留其中的某些文件夾。 我試過正則表達式匹配這樣.hgignore除一些子文件夾外的文件夾

syntax: regexp 
^site/customer/\b(?!.*/data/.*).* 

可惜,這是行不通的。 我在這answer中讀到python只能進行固定寬度的負向查找。

我希望忽略不可能嗎?

回答

0

Python的正則表達式是酷

Python做支持負先行查找(?=.*foo)。但它不支持任意長度的負向lookbehind查找(?<=foo.*)。它需要被修復(?<=foo..)

這意味着它絕對有可能解決您的問題。

問題

你有以下的正則表達式:/customer/(?!.*/data/.*).*
我們來看一個輸入示例/customer/data/name。它匹配的原因。

/customer/data/name 
^^^^^^^^^^ -> /customer/ match ! 
     ^(?!.*/data/.*) Let's check if there is no /data/ ahead 
      The problem is here, we've already matched "/" 
      so the regex only finds "data/name" instead of "/data/name" 
      ^^^^^^^^^ .* match ! 

修復你的正則表達式

基本上,我們只需要刪除一個斜槓,我們添加錨點^,以確保它是字符串的開始,並確保我們只需使用\b匹配customer^/customer\b(?!.*/data/).*

Online demo

+0

您好,感謝您的回答。 我的文件夾包含不同客戶的文件夾,這些文件夾可以再次包含裏面的數據文件夾。理論上我的正則表達式應該匹配那些。 我試圖修復左側,就像你這樣做: ^ site/customer/\ b(?!。*/data /.*).* 但它也不起作用。 – Niksac

+0

@Niksac請提供[regex101](http://regex101.com)演示。另請注意,我使用了'^/customer \ b(?!。*/data /)。*'而不是'^ site/customer/\ b(?!。*/data /.*).*'。 (你已經添加了正斜槓) – HamZa

+0

正則表達式工程http://regex101.com/r/pG9mV1但hg仍然忽略所有文件 – Niksac

相關問題