Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing specific words in a string (Python)

I would like to replace words in a string sentence such as:

What $noun$ is $verb$?

What's the regular expression to replace the characters in '$ $' (inclusive) with actual nouns/verbs?

like image 324
ono Avatar asked Sep 21 '12 21:09

ono


1 Answers

You don't need a regular expression for that. I would do

string = "What $noun$ is $verb$?"
print string.replace("$noun$", "the heck")

Only use regular expressions when needed. It's generally slower.

like image 74
Ztyx Avatar answered Oct 22 '22 03:10

Ztyx