I would like to utilize user input to match and rearrange strings. In Perl a simple example would look like the following:
use strict;
my $str = 'abc123def456ghi';
my $user_input1 = '(\d+).+?(\d+)';
my $user_input2 = '$2.$1';
if ($str =~ /$user_input1/) {
  my $newstr = eval($user_input2);
  print $newstr;
}
else {print "No match..."};
The same works in principle also in Python:
import re
mystr = 'abc123def456ghi'
user_input1 = '(\d+).+?(\d+)'
user_input2 = 'm.group(2) + m.group(1)'
m = re.search(user_input1,mystr)
if m:
    newstr = eval(user_input2)
    print (newstr)
else: print ("No match...")
Result: 456123
However, the expressions 'm.group(1)' and 'm.group(2)' are not very user-friendly if you have to type it several times in an entry field.
Therefore, I am wondering if in Python there are similar compact expression like '$1' '$2' in Perl?
I can't get it work in Python with '\1' and '\2'.
Any ideas?
Edit:
Sorry, I try to explain after some comments below:
I was trying eval() since it seems to work with m.group(1) etc.
but apparently for some reason r'\1' etc. is not accepted in eval()
import re
mystr = 'abc123def456ghi'
user_input1 = r'(\d+).+?(\d+)'
user_input2 = r'\2\1'
newstr = eval(user_input2)
print (newstr)
results in
SyntaxError: unexpected character after line continuation character
About the suggestion to use re.sub()
It should not be a simple substitution of a string but a rearrangement+addition of matches. If I modify the original regex
user_input1 = r'(.+?)(\d+).+?(\d+)(.+)'
I can use user_input2 = r'\3\2'
However, e.g. if I want to add '999' inbetween the matches (of course, neither r'\3999\2' or r'\3+"999"+\2' does it) then probably I am back using eval() and m.group(1) etc. although I wanted to keep the user input as short and simple as possible. Maybe, I can use some kind of the suggested substitutions.
 
    