Using regex to remove all text after the last number in a string

后端 未结 2 2017
有刺的猬
有刺的猬 2021-01-25 00:25

Sample Text

1234 Main St Smallville, KS 92348Small County 

Should yield:

1234 Main St Smallville, KS 92348

Sa

2条回答
  •  孤城傲影
    2021-01-25 01:20

    You can use greedy .* before \d+ to make sure to match upto last digits:

    >>> print re.findall(r'^(.*\d+).*$', address)[0]
    1234 Main St Smallville, KS 92348
    

    Regex Demo

提交回复
热议问题