regex - Python match and replace, what I do wrong? -
i have reg exp match data (is here) , try replace matched data single :
characetr
test_str = u"there 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 on few other way it's not replace or replace not matched 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 replacing pattern :\1
, means replace text :
followed first group in regex.
replace first group inside text should add 2 groups , before first , after. hope fix issue:
test_str = u"there 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)
Comments
Post a Comment