Change the enclosing character of the regular expression - m //

Perl allows you to change the enclosing character of a regular expression.

Regular expression could be written as follows by combining pattern matching operators.

$string =~ /cat/</pre>

But now, suppose you want to match the path of a URL, for example. You can escape the slash and write:

<pre>
$string =~ /\/foo \/bar \/baz/</pre>

Isn't it a little difficult to read? In Perl, there is a way to change the regular expression enclosing character "/" to another enclosing character. Write as follows.

<pre>
$string =~ m |/foo/bar/baz |

Prefix it with an "m" to change the enclosing character to something else. The following combinations can be used as enclosing characters.

m | regular expression |
# m regular expression #
m! Regular expression!

It can be used in the same way when replacing with s.

s | Regular expression | Replaced string |
# s Regular expression # Replaced string #
s! Regular expression! Replaced string!

Related Informatrion