0

Very simple question but I can't seem to figure it out.

The following code:

import re
addr = '800 W NORTH AVE'
re.sub(r'([a-zA-Z]+)', 'W North A', addr)

It gives me as a result 800 W North A W North A W North A instead of 800 W North A

I don't understand what am I doing wrong. Would appreciate any help.

Thanks

2
  • There's a tool included in c:\python27\tools\scripts\redemo.py (season to your environment) that will visualize regex matching for you. Commented Dec 10, 2016 at 20:36
  • I'm not sure what exactly your really trying to do, but for the above you could do '800 W NORTH AVE'.title() instead... Commented Dec 10, 2016 at 20:44

2 Answers 2

2

You are not matching the space character. This makes every word get replaced with the replace string. You need something like this instead:

re.sub(r'(([a-zA-Z]+\?)+)', 'W North A', addr)

This matches one or more of a word followed by one or more spaces.

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

Comments

0

Like user92454 says, you want to use a space character, which is \s in Python regex.

You can use the pattern \s([A-Z\s]+) if you know the text you want to replace is always in caps. If not, you can use \s([A-z\s]+) (lower case 'z').

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.