NoctemX NoctemX - 1 month ago 9
Python Question

Need a little regex help in python

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.

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).


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.

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!

Answer

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)\.)

Comments