Reputation: 840
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
Reputation: 23
import re
re.search("\d\d+.*","some rubish 2 some more rubish 2403 some street address, TX 77630409 asdsad").group()
Upvotes: 1
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
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