0

I'm finding it difficult getting started with regular expressions in python.

I have a bunch of strings now that look like this:

<street address><SPACE><#><SPACE><Suite number or letter>

I need to separate the Suite number from the rest of the string and save it in another variable. I also need a copy of the street address without the suite number.

Here are some examples:

    1111 19th St NW # 200
    1408 U St NW # A
    1509 17th St NW # 1
    1515 14th St NW # 1
    1612 K St NW # 1000
    1700 17th St NW # C
    1900 K St NW # 1200
    1900 M St NW # 200
    6034 Baltimore Ave # 2
    843 Quarry Road # 140
    8455 Colesville Rd # 100

What is a good way to do this?

Thanks!

3 Answers 3

2

Something like this work for you?

s = '1111 19th St NW # 200'
n = s.split('#')[1].strip()

print n

http://ideone.com/OnaqX

Sign up to request clarification or add additional context in comments.

Comments

2
s.split()[-1]

This will return the entirety of the string after the last space.

With a few of your strings as examples:

>>> L = ["1111 19th St NW # 200",
    "1408 U St NW # A",
    "1509 17th St NW # 1",
    "1515 14th St NW # 1"]
>>> [s.split()[-1] for s in L]
['200', 'A', '1', '1']

Comments

2

Well, I'm not sure how variable your input can be, but either one of these patterns would grab the suite numbers from the examples you gave:

/\w+$/

/\#\s*(\w+)/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.