string - Python replace different character -
i have different strings these :
"/table[1]/tr/td[2]/table[2]/tr/td[2]/p/b/text()" "/table[1]/tr/td[2]/table[3]/tr/td[2]/p/b/text()" i'd change substring "/table[" + number + "]" "/table[" + same number + "]/tbody".
for example string
"/table[1]/tr/td[2]/table[2]/tr/td[2]/p/b/text()" should change in
"/table[1]/tbody/tr/td[2]/table[2]/tbody/tr/td[2]/p/b/text()"
use symbolic group naming, way:
>>> s '/table[1]/tr/td[2]/table[2]/tr/td[2]/p/b/text()' >>> >>> re.sub(r'(?p<table>/table\[\d+\])', r'\g<table>/tbody', s) '/table[1]/tbody/tr/td[2]/table[2]/tbody/tr/td[2]/p/b/text()' >>> >>> #similarly can reference group number >>> re.sub(r'(?p<table>/table\[\d+\])', r'\g<1>/tbody', s) '/table[1]/tbody/tr/td[2]/table[2]/tbody/tr/td[2]/p/b/text()' quoting python doc:
(?p<name>...)
similar regular parentheses, substring matched group accessible via symbolic group name name. group names must valid python identifiers, , each group name must defined once within regular expression. symbolic group numbered group, if group not named.
Comments
Post a Comment