简体   繁体   English

字符串替换

[英]substitution in string

I have the following string ./test and I want to replace it with test so, I wrote the following in perl: my $t =~ s/^.//; 我有以下字符串./test ,我想用test替换它,因此,我在perl中编写了以下代码: my $t =~ s/^.//; however, that replaces ./test with /test can anyone please suggest how I fix it so I get rid of the / too. 但是,用/test替换./test可以有人建议我如何解决它,以便也摆脱/ thanks! 谢谢!

my $t =~ s/^\.\///;

You need to escape the dot and the slash. 您需要转义圆点和斜线。

The substitution is s/match/replace/ . 替换为s/match/replace/ If you erase, it's s/match// . 如果删除,则为s/match// You want to match "starts with a dot and a slash", and that's ^\\.\\/ . 您要匹配“以点和斜杠开头”,即^\\.\\/

The dot doesn't do what you expect - rather than matching a dot character, it matches any character because of its special treatment. 点不符合您的期望-与其匹配点字符,不如匹配任何字符,因为它经过特殊处理。 To match a dot and a forward slash, you can rewrite your expression as follows: 要匹配点和正斜杠,可以按如下方式重写表达式:

my $t =~ s|^\./||;

Note that you are free to use a different character as a delimiter, in order not to confuse it with any such characters inside the regular expression. 请注意,您可以随意使用其他字符作为分隔符,以免将其与正则表达式内的任何此类字符混淆。

If you want to get rid of ./ then you need to include both of those characters in the regex. 如果要删除./则需要在正则表达式中包括这两个字符。

s/^\.\///;

Both . 两者都有. and / have special meanings in this expression ( . is a regex metacharacter meaning "any character" and / is the delimiter for the s/// operator) so we need to escape them both by putting a \\ in front of them. /在此表达式中具有特殊含义( .是一个正则表达式元字符,表示“任何字符”,并且/s///运算符的分隔符),因此我们需要通过在它们前面加上\\来对它们进行转义。

An alternative (and, in my opinion, better) approach to the / issue is to change the character that you are using as the s/// delimiter. 解决/问题的另一种方法(我认为更好)是更改用作s///分隔符的字符。

s|^\./||;

This is all documented in perldoc perlop . 这全部记录在perldoc perlop中

You have to use a backward slash before the dot and the forward slash: s/\\.\\//; 您必须在点和正斜杠之前使用反斜杠:s /\\.\\//; The backslash is used to write symbols that otherwise would have a different meaning in the regular expression. 反斜杠用于编写符号,否则这些符号在正则表达式中将具有不同的含义。

You need to write my $t =~ s/^\\.\\///; 您需要写my $t =~ s/^\\.\\///; (Note that the period needs to be escaped in order to match a literal period rather than any character). (请注意,需要转义句点以匹配文字句点而不是任何字符)。 If that's too many slashes, you can also change the delimiter, writing instead, eg, my $t =~ s:^\\./::; 如果斜杠太多,您也可以更改定界符,改写例如, my $t =~ s:^\\./::; .

$t=q(./test);$t=~s{^\./}{};print $t;

You need to escape the dot if you want it to match a dot. 如果要与点匹配,则需要对点进行转义。 Otherwise it matches any character. 否则,它匹配任何字符。 You can choose alternate delimiters --- best when dealing with forward slashes lest you get the leaning-toothpick look when you otherwise need to escape those too. 您可以选择其他定界符---最好在处理正斜杠时,以免在其他情况下也需要转义时避免出现牙签的外观。

Note that the dot in your question is matching any character, not a literal '.'. 请注意,问题中的点与任何字符都匹配,而不是文字“。”。

my $t = './test';
$t =~ s{\./}{};
use Path::Class qw( file );
say file("./test")->cleanup();

Path::Class 路径::类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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