hleggs hleggs - 5 months ago 18
Python Question

Strip everything until any number (0-9) is reached?

How can I strip everything leading up to a number? Of course this would have to work for any number, not just 1.

I would like

blahblahblah 1 main street
as input. And
1 main street
as output.

Answer

You can use the re module's sub function to replace all non-numerical characters from the left side.

import re

s = 'blahblahblah 1 main street'
re.sub(re.compile(r'^[^0-9]*'), '', s)

#Outputs '1 main street'