简体   繁体   中英

Python regex substitute from some position of pattern

I have a function:

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

and I want to find some pattern like this " Word " (with prefix/suffix spaces) and replace it to "Word" (without those spaces). in ruby I did it before with something like this:

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

Edit: I mean I need to change those spaces with a new string or something else depend on situation.

The problem is that Python is interpreting your '\\1' as a special backslashed character; you need to use a raw string , which can be done by adding an r immediately before the string. 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'

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM