python-perl icon indicating copy to clipboard operation
python-perl copied to clipboard

$1 after s/// not working

Open ruppde opened this issue 5 years ago • 0 comments

Hello,

would be great if your module would be able to do get the grouped characters from a substitution regex like this perl script does:

#!/usr/bin/perl
$sentence = "This is whatever.";
$sentence =~ s/([\.,:;]$)//;
$remove_punctuation_mark = $1;
print "<$sentence><$remove_punctuation_mark>\n";

I didn't find any way to do this with the normal python regex. With your perl module I would expect this to work:

#!/usr/bin/python3.8 -mperl
sentence = "This is whatever."
sentence =~ s/([\.,:;])$//
remove_punctuation_mark = $1
print(t, remove_punctuation_mark)

But this gives 2 errors:

  1. Error: sentence =~ s/([\.,:;])$// ^ SyntaxError: unexpected character after line continuation character

  2. If the regex is changed to e.g. s/([.,:;])$/X/ it throws the error: NameError: name '__perl__var__1' is not defined

ruppde avatar Feb 25 '20 21:02 ruppde