Python regular expression replaces some pattern position

I have a function:

find = re.compile(ur"\s+(Word)\s+", flags = re.U) text = find.sub('\1', text) 

and I want to find a template like this " Word " (with a prefix / suffix) and replace it with "Word" (without these spaces). in ruby, I did this before with something like this:

 text.gsub('\s+(Word)\s+', '\1') 

Edit: I mean, I need to change these spaces with a new line or something else depending on the situation.

+4
source share
2 answers

The problem is that Python interprets your '\ 1' as a special backward character; you need to use raw string , which can be done by adding r immediately before the string. The change

 find.sub('\1', text) 

to

 find.sub(r'\1', text) 

Example:

 text = "Replace this Word " find = re.compile(ur"\s+(Word)\s+", flags = re.U) find.sub(r'\1', text) # 'Replace thisWord' 
+3
source

Try the following:

 regcom = re.compile('\s+Word\s+', re.UNICODE) print regcom.sub(u'Word', u'This is a Word ') u'This is aWord' 
+1
source

All Articles