Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "s!" operator in Perl do?

Tags:

python

regex

perl

I have this Perl snippet from a script that I am translating into Python. I have no idea what the "s!" operator is doing; some sort of regex substitution. Unfortunately searching Google or Stackoverflow for operators like that doesn't yield many helpful results.

 $var =~ s!<foo>.+?</foo>!!;
 $var =~ s!;!/!g;

What is each line doing? I'd like to know in case I run into this operator again.

And, what would equivalent statements in Python be?

like image 417
Casey Avatar asked Nov 26 '22 22:11

Casey


1 Answers

s!foo!bar! is the same as the more common s/foo/bar/, except that foo and bar can contain unescaped slashes without causing problems. What it does is, it replaces the first occurence of the regex foo with bar. The version with g replaces all occurences.

like image 194
sepp2k Avatar answered Nov 29 '22 13:11

sepp2k