I have reg exp for match some data (is it here ) and now I try to replace all matched data with single :
characetr
test_str = u"THERE IS MY DATA"
p = re.compile(ur'[a-z]+([\n].*?<\/div>[\n ]+<div class="large-3 small-3 columns">[\n ]+)[a-z]+', re.M|re.I|re.SE)
print re.sub(p, r':/1',test_str)
I try it on few other way but it's not replace any or replace not only matched but whole pattern
1)It's backslash issue.
Use : print re.sub(p, r':\\1',test_str)
not print re.sub(p, r':/1',test_str)
.
2)You are replacing all the pattern with :\\1
, that means replace all the text with :
followed by the first group in the regex.
To replace just the first group inside the text you should add two groups , before the first and after. I hope this will fix the issue:
test_str = u"THERE IS MY DATA"
p = re.compile(ur'([a-z]+)([\n].*?<\/div>[\n ]+<div class="large-3 small-3 columns">[\n ]+)([a-z]+)', re.M|re.I|re.SE)
print re.sub(p, r'\1:\2\3',test_str)
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.