hleggs hleggs - 1 year ago 64
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 Source

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'