What does "s!" Mean operator in Perl?

I have this Perl fragment from a script that I translated into Python. I have no idea what it is! the operator does; something like replacing regular expressions. Unfortunately, a Google or Stackoverflow search for such operators does not provide many useful results.

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

What does each line do? I would like to know if I came across this operator again.

And what would be the equivalent statements in Python?

+4
source share
7 answers

s!foo!bar! matches the more general s/foo/bar/ , except that foo and bar can contain unrelated slashes without causing problems. What he does is he replaces the first appearance of the regular expression foo in the panel. The version with g replaces all occurrences.

+15
source

It does the same as $var =~ s/// . i.e., search and replace in the $var variable.

In Perl, you can define the delimiter character following s . What for? For example, if you match "/", you can specify another separator character (in this case, "!"), And you do not need to run or return the character that you match. Otherwise, you get (say)

 s/;/\//g; 

which is a bit confusing.

Perlre provides more information about this.

+13
source

Perl lets you choose a separator for many of your designs. This makes it easier to see what happens in type expressions.

 $str =~ s{/foo/bar/baz/}{/quux/}; 

As you can see, not all delimiters have the same effects. Bracketing characters ( <> , [] , {} and () ) use different characters for the start and end. And ? , when used as a delimiter for a regular expression, causes regular expressions to match only once between calls to the reset() operator.

You might find it useful to read perldoc perlop (specifically the sections m/PATTERN/msixpogc ?PATTERN? And s / PATTERN / REPLACEMENT / msixpogce ).

+10
source

s is the substitution operator. Usually it is in the form s/foo/bar/ , but you can replace the // characters with a separator with other characters like !. Using other delimiter delimiters can make things easier, such as paths, since you don't need to avoid path delimiters.

See the man page for more details.

You can find similar functionality for python in re-module .

+3
source

s is the substitution operator. Typically, the delimiter uses '/':

 s/foo/bar/ 

but this is not required: instead, several other characters can be used as delimiters. In this case, '!' was used as a delimiter, apparently to avoid having to avoid the "/" characters in the actual text to be replaced.

In your particular case, the first line removes the text match '. +? '; those. removes foo tags with or without content.

The second line replaces everything with ';' characters with the characters "/", globally (all occurrences).

The python equivalent uses the re module:

 f=re.sub(searchregx,replacement_str,line) 
+2
source

s! is the syntactic sugar for the correct s/// operator. Basically, you can replace any delimiter you want instead of "/".

Depending on what each line does, the first line matches the matches of the regular expression <foo>.+?</foo> and replaces the entire batch with nothing. The second matches the regular expression ; and replaces it with / .

s/// is a replacement operator. It accepts regular expression and wildcard.

 s/regex/replace string/; 

It supports most (all?) Regular regex switches that are used in the usual way (by adding them to the end of the statement).

+2
source

And the python equivalent should use the re module.

0
source

All Articles