简体   繁体   中英

perl regular expressions substitue

I'm new to perl and I found this expressions bit difficult to understand

$a =~ s/\'/\'\"\'\"\'/g

Can someone help me understand what this piece of code does?

It is not clear which part of it is a problem. Here is a brief mention of each.

The expression $str =~ s/pattern/replacement/ is a regular expression , replacing the part of string $str that is matched by the "pattern", by the "replacement". With /g at the end it does so for all instances of the "pattern" found in the string. There is far, far more to it -- see the tutorial , a quick start , the reference , and full information in perlre and perlop .

Some characters have a special meaning, in particular when used in the "pattern". A common set is .^$*+?()[{\\| , but this depends on the flavor of regex. Also, whatever is used as a delimiter (commonly / ) has to be escaped as well. See documentation and for example this post . If you want to use any one of those characters as a literal character to be found in the string, they have to be escaped by the backslash. (Also see about escaped sequences .) In your example, the ' is escaped, and so are the characters used as the replacement.

The thing is, these in fact don't need to be escaped. So this works

use strict;
use warnings;

my $str = "a'b'c";
$str =~ s/'/'"'/g;
print "$str\n";

It prints the desired a'"'b'"'c . Note that you still may escape them and it will work. One situation where ' is indeed very special is in a one liner , where it delimits the whole piece of code to submit to Perl via shell, to be run. (However, merely escaping it there does not work.) Altogether, the expression you show does the replacement just as in the answer by Jens , but with a twist that is not needed.

A note on a related feature: you can use nearly anything for delimiters, not necessarily just / . So this |patt|repl| is fine, and nicely even this is: {patt}{repl} (unless they are used inside). This is sometimes extremely handy, improving readability a lot. For one thing, then you don't have to escape the / itself inside the regex.

I hope this helps a little.

All the \\ in that are useless (but harmless), so it is equivalent to s/'/'"'"'/g . It replaces every ' with '"'"' in the string.

This is often used for shell quoting. See for example https://stackoverflow.com/a/24869016/17389

这个代码的和平取代每个单引号与'"'"'

Can be simplified by removing the needless back slashes $a =~ s/'/"'"'/g

Every ' will we replaced with "'"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM