Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16714

Shorten regex to capture digits grouped by variable delimiter

$
0
0

I have a regex that is trying to capture integer numbers like

12_456_789

But the delimiter can by either any of [\ _.,] or no delimiter. My regex looks like

\d{1,3}((_\d{3})*|(\ \d{3})*|(\.\d{3})*|(,\d{3})*|(\d{3})*)

This regex works as epxected, but it's annoying having to repeat myself for each delimiter. I'm tempted to do something like

\d{1,3}(([\ _.,]\d{3})*)

But this would match a string like

123,456.789_987

which is not acceptable.

Is there some kind of abbreviation along the lines of my temptation that can be used? Or am I stuck with conjunction repetition?

I'm working in python verbose regex.


Viewing all articles
Browse latest Browse all 16714

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>