在python中需要一些regex帮助

时间:2022-08-26 12:15:41

I have a long list of namespaces coming from a graphite instance and am trying to run some validation and filter out the namespaces that have not been entered using the correct format.

我有一长串来自一个石墨实例的名称空间,并且正在尝试运行一些验证,并过滤掉没有使用正确格式输入的名称空间。

I can do ok with regex usually, but this one is hurting me and was hoping someone could help.

我通常可以用regex做得很好,但是这个会伤害我,希望有人能帮忙。

There is some basic validation that needs to be done and I've come this far:

有一些基本的验证需要做,我已经走到了这一步:

^(stats\.gauges\..*\.(?:dur|hop|scz|led|bgi|shi|crk|sas)\.(?:p|s|q|u|d|x)\.)

The goal is to filter and match on the ones following the standardized stats.gaugues.*.(exact OR match).(exact OR match).

目标是根据标准化的stats.gaugues.*过滤和匹配。(精确匹配)。(精确匹配)。

This works ok for something like:

这种方法在以下情况下是可行的:

stats.gauges.gitswarm.dur.p.durgitswarm1.service.repos.eeps-merge_demo.users

But unfortunately someone entered some like:

但不幸的是,有人输入了如下内容:

stats.gauges.gitswarm.dur.p.durgitswarm1.dur.p.service.repos.eeps-merge_demo.users

So these match all the way to the second dur.p. skipping possible error on exact matches such as stats.gauges.gitswarm.durham.p.durgitswarm1.dur.p.service.repos.eeps-merge_demo.users; which I don't want.

所以这些匹配一直到第二个。p。跳过精确匹配上可能出现的错误,如状态。量规。gitswarem . durhamp.durgitswarm1 .dur.p.service.repos.eep -merge_demo.users;我不想要。

I feel like this is something pretty simple, but can't seem to grasp it so far.

我觉得这是相当简单的事情,但似乎还不能理解。

Any help would be very much appreciated.

非常感谢您的帮助。

Thank you!

谢谢你!

1 个解决方案

#1


2  

This should work. Basically you need to make your .* non greedy ^(stats\.gauges\..*?\.(?:dur|hop|scz|led|bgi|shi|crk|sas)\.(?:p|s|q|u|d|x)\.)

这应该工作。基本上,你需要使你的。*非贪婪^(统计\ .gauges \ . . * ? \。(?:大调的|跳| scz华大基因研究院领导| | |施| crk | sas)\。(?:d u p s | | | | | x)\)。

#1


2  

This should work. Basically you need to make your .* non greedy ^(stats\.gauges\..*?\.(?:dur|hop|scz|led|bgi|shi|crk|sas)\.(?:p|s|q|u|d|x)\.)

这应该工作。基本上,你需要使你的。*非贪婪^(统计\ .gauges \ . . * ? \。(?:大调的|跳| scz华大基因研究院领导| | |施| crk | sas)\。(?:d u p s | | | | | x)\)。