Dimebag Dimebag - 2 months ago 8
Python Question

Regex pattern within text

I have a long string of data which looks like:

dstgfsda12345.123gsrsvrvsdfcsd23456.234tsrsd


Notice that the '12345.123' pattern is the same. I want to split the string on it using python (so
s.split(<regex>)
).

What would be the appropriate regex?

'[0-9]{5}.[0-9]{3}'


does not work; I presume it expects whitespace around it(?).

Answer

Just escape ., and you are done:

\d{5}\.\d{3}

You can use Regex token \d as a shorthand for [0-9].

Example:

>>> re.split(r'\d{5}\.\d{3}', 'dstgfsda12345.123gsrsvrvsdfcsd23456.234tsrsd')
['dstgfsda', 'gsrsvrvsdfcsd', 'tsrsd']