Ajay Chinni
Ajay Chinni

Reputation: 840

left strip anything before the second number in a string

a = "some rubish 2 some more rubish 2403 some street address, TX 4377435"

This is some address string with some extra information I want to strip everything before the street number which is always the 2nd number from the right. I want output as

"2403 some street address, TX 4377435"

Till now I could come up with this

special_char = '!@#$%^&*()-_=+,./\\;:<>?{}[]|    '
ascii_letters  = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
a = "some rubish 2 some more rubish 2403 some street address, TX 77630409"
a.lstrip(ascii_letters+special_char)

Which gives output

2 some more rubish 2403 some street address, TX 77630409

Upvotes: 0

Views: 129

Answers (3)

Ajaykumar Manimala
Ajaykumar Manimala

Reputation: 23

import re    

re.search("\d\d+.*","some rubish 2 some more rubish 2403 some street address, TX 77630409 asdsad").group()

Upvotes: 1

Guy
Guy

Reputation: 50819

You can find the index of the second number from the right using re and [-2] index and slice the string by it

a = "some rubish 2 some more rubish 2403 some street address, TX 4377435"
index = re.findall(r"\d+", a)[-2]
print(a[a.index(index):]) # 2403 some street address, TX 77630409

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

Here is a regex splitting option. We can split the input string at any space which is followed by a digit (using a lookahead to avoid consuming that digit). Then, splice off the first two elements and retain the rest. Finally, join back to a single string by reintroducing the space.

a = "some rubish 2 some more rubish 2403 some street address, TX 4377435"
parts = re.split(r'[ ](?=\d)', a)
print(' '.join(parts[2:]))  # 2403 some street address, TX 4377435

Upvotes: 8

Related Questions