[英]RegEx : Match all lines except for a specific sub-string
Below is the list :以下是列表:
cf-ab1
cf-bc2
cf-ab1-hotfix
cf-bc2-hotfix
cf-ab1-canary
cf-cd1-staging
cf-cd1-staging2
cf-cd1
cf-cd1-sic-staging
cf-cd1-sagdf-staging
I would like to match everything except for cf-cd1-staging, cf-cd1-staging2 and cf-ab1-canary
I am running the below regex :我想匹配除
cf-cd1-staging, cf-cd1-staging2 and cf-ab1-canary
我正在运行以下正则表达式:
^((?!canary|staging).)*$
But these ideally matches all lines that doesnot contain staging and canary..!但是这些理想情况下匹配所有不包含 staging 和 canary 的行..! which should not be my desired o/p.
这不应该是我想要的 o/p。
Could you please help here..!?你能在这里帮忙吗..!? because my desired matches should be :
因为我想要的比赛应该是:
cf-ab1
cf-bc2
cf-ab1-hotfix
cf-bc2-hotfix
cf-cd1
cf-cd1-sic-staging
cf-cd1-sagdf-staging
Regards,问候,
Rohith罗希特
Try this : -尝试这个 : -
import re
lines = ["cf-ab1", "cf-bc2", "cf-ab1-hotfix", "cf-bc2-hotfix", "cf-ab1-canary", "cf-
cd1-staging", "cf-cd1-staging2", "cf-cd1", "cf-cd1-sic-staging", "cf-cd1-sagdf-
staging"]
line_compile = re.compile('^(?!.*(ab1-canary|cd1-staging|cf-ab1-canary)).*$')
matched = []
for line in lines:
if line_compile.match(line):
matched.append(line)
As always with RegEx, there's many possible solutions.与 RegEx 一样,有许多可能的解决方案。 I came up with one on the fly but you could argue that it's overfitted to that dataset and not very generalized.
我很快就想出了一个,但你可能会争辩说它过拟合到那个数据集并且不是很普遍。
^cf-\w\w\d(-[hs][oia][tcg].+?)?$
I simply wrote all the "allowed" letters in square brackets until the undesired matches weren't possible anymore.我只是在方括号中写了所有“允许”的字母,直到不再可能出现不需要的匹配为止。 Also, I put the second half in ()?
另外,我把后半部分放在()? so that the two short entries are also matched.
这样两个短条目也匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.