【发布时间】:2023-03-29 07:15:01
【问题描述】:
我想忽略一个文件夹,但保留其中的一些文件夹。 我试过这样的正则表达式匹配
syntax: regexp
^site/customer/\b(?!.*/data/.*).*
不幸的是,这不起作用。 我在answer 中读到,python 只进行固定宽度的负查找。
我想要的忽略是不可能的吗?
【问题讨论】:
标签: regex mercurial tortoisehg regex-negation
我想忽略一个文件夹,但保留其中的一些文件夹。 我试过这样的正则表达式匹配
syntax: regexp
^site/customer/\b(?!.*/data/.*).*
不幸的是,这不起作用。 我在answer 中读到,python 只进行固定宽度的负查找。
我想要的忽略是不可能的吗?
【问题讨论】:
标签: regex mercurial tortoisehg regex-negation
Python 确实支持负 lookahead 查找 (?=.*foo)。但它不支持任意长度的负向后查找(?<=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/).*。
【讨论】:
^/customer\b(?!.*/data/).* 而不是^site/customer/\b(?!.*/data/.*).*。 (您已添加正斜杠)
customer 之后添加/ ... sigh 尝试删除^。我不熟悉hg ...