Created
October 22, 2015 07:44
-
-
Save jinstrive/45617d8654963d62d2a7 to your computer and use it in GitHub Desktop.
python string operate, replace string in Regular Expression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def replace_matched_part(pattern, lstring, repl_str, group_name): | |
""" | |
:param pattern: 类似 '.*?_(?P<char>\w)_\d\.\w+$' | |
:param lstring: 需要替换的字符串 | |
:param repl_str: 匹配部分替换成的字符串 | |
:param group_name: 匹配中的组名 char | |
:return: | |
""" | |
def _rep_func(m): | |
return ''.join([m.string[:m.start(group_name)], repl_str, m.string[m.end(group_name):]]) | |
return re.sub(pattern, _rep_func, lstring) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment