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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -