Python Regex instantly replace groups -
is there way directly replace groups in regex-syntax.
the normal way:
re.match(r"(?:aaa)(_bbb)", string1).group(1)
yet want achieve this:
re.match(r"(\d.*?)\s(\d.*?)", "(call_group_1) (call_group_2)")
so want build new string instantaneous if possible calling groups regex caught.
have @ re.sub
:
result = re.sub(r"(\d.*?)\s(\d.*?)", r"\1 \2", string1)
this python's regex substitution (replace) function. replacement string can filled so-called backreferences (backslash, group number) replaced matched groups. groups counted same group(...)
function, i.e. starting 1
, left right, opening parentheses.
Comments
Post a Comment