1

I have a string: "y, i agree with u."

And I have array dictionary [(word_will_replace, [word_will_be_replaced])]:

[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]

i want to replace 'y' with 'yes' and 'u' with 'you' according to the array dictionary.

So the result i want: "yes, i agree with you."

I want to keep the punctuation there.

3
  • related: stackoverflow.com/questions/16516623/… Commented May 17, 2013 at 3:17
  • regular-expressions.info/reference.html The \b meta character matches on word boundaries, as in, between a word and space or word and symbol. e.g. \by\b will match ONLY the word y on its own. Commented May 17, 2013 at 3:17
  • Both solutions below are correct under the assumption that the replacement word is not a word to be replaced for another word. e.g. {abc <-- ab, abcd <-- abc}. If you don't have this assumption, then only jamylak's solution is correct. Commented May 17, 2013 at 4:01

3 Answers 3

3
import re
s="y, i agree with u. yu."
l=[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])] 
d={ k : "\\b(?:" + "|".join(v) + ")\\b" for k,v in l}
for k,r in d.items(): s = re.sub(r, k, s)  
print s

Output

yes, i agree with you. you.
Sign up to request clarification or add additional context in comments.

3 Comments

This is a good example. If you're going to do this over and over again, it might be worthwhile to store the dictionary as key:re.compile(...)
how I can make d={'yes': ['y', 'ya', 'ye'],'you': ['u', 'yu']} from the array [('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])] ??
@mgilson that is superfluous, as compiling and caching regexes is already built into the re module.
2

Extending @gnibbler's answer from Replacing substrings given a dictionary of strings-to-be-replaced as keys and replacements as values. Python with the tips implemented from Raymond Hettinger in the comments.

import re
text = "y, i agree with u."
replacements = [('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
d = {w: repl for repl, words in replacements for w in words}
def fn(match):
    return d[match.group()]

print re.sub('|'.join(r'\b{0}\b'.format(re.escape(k)) for k in d), fn, text)

>>> 
yes, i agree with you.

1 Comment

@perreal no since those aren't captured
0

That's not a dictionary -- it's a list, but it could be converted to a dict pretty easily. In this case, however, I would make it a little more explicit:

d = {}
replacements = [('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
for value,words in replacements:
    for word in words:
        d[word] = value

Now you have the dictionary mapping responses to what you want to replace them with:

{'y':'yes', 'ya':'yes', 'ye':'yes',...}

once you have that, you can pop in my answer from here using regular expressions: https://stackoverflow.com/a/15324369/748858

1 Comment

Sorry, when I want to replace: "You" there become: "Yesou"

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.