$stuff = "d:/learning/perl/tmp.txt";
open STUFF, $stuff or die "Cannot open $stuff for read :$!";
while (<STUFF>) {
my($line) = $_; # Good practice to always strip the trailing
chomp($line);
my @values = split(' ', $line);
foreach my $val (@values) {
if ($val == 1){
print "1 found";
}
elsif ($val =~ /hello/){
print "hello found";
}
elsif ($val =~ /"/*"/){ # I don't know how to handle here.
print "/* found";
}
print "\n";
}
}
My tmp.txt
:
/* CheerStone ColdStunner
1 Cheer Rock
hello Boo Pedigree
How do I handle the /*
character sequence in my code?
如何处理代码中的/ *字符序列?
3 个解决方案
#1
8
Both characters have special meaning in Perl regular expression, so you have to escape them with a backslash:
这两个字符在Perl正则表达式中都有特殊含义,因此您必须使用反斜杠转义它们:
$val =~ /\/\*/
Btw, you should probably add ^
in front of all the regexes, because you seem to want to handle only text on the beginning of the line.
顺便说一下,你应该在所有正则表达式前添加^,因为你似乎只想处理行开头的文本。
#2
16
The *
is a special character. So, you have to escape it:
*是一个特殊字符。所以,你必须逃脱它:
m{/\*}
As Adam Bellaire has suggested, here is a brief explanation:
正如Adam Bellaire建议的那样,这是一个简短的解释:
Picket-fences are best avoided. For that, sometimes, delimiters other than /
have to be used. m
should precede the first delimiter when using such delimiters. If any of the brackets are used as the first delimiter, the corresponding closing bracket has to be used as the end delimiter.
最好避免使用栅栏。为此,有时候,除了/必须使用分隔符。使用此类分隔符时,m应位于第一个分隔符之前。如果任何括号用作第一个分隔符,则必须使用相应的闭合括号作为结束分隔符。
#3
12
There are various ways to un-meta regex metacharacters. In your case, you need to handle the character that is the default delimiter as well as a meta-character.
un-meta正则表达式元字符有多种方法。在您的情况下,您需要处理作为默认分隔符的字符以及元字符。
-
In the case of a delimiter character that you want to be a literal character, you can escape that character with \, although you can get leaning toothpick syndrome:
对于你想成为文字字符的分隔符,你可以用\来逃避那个角色,虽然你可以得到倾斜的牙签综合症:
m/\/usr\/local\/perls/;
-
You can change the delimiter:
您可以更改分隔符:
m(/usr/local/perl);
-
You can escape meta-characters in the same way:
您可以以相同的方式转义元字符:
m(/usr/local/perl\*);
-
If you want a section of your pattern to be only literal characters, you can use
\Q
to automatically escape them for you:如果您希望模式的某个部分只是文字字符,则可以使用\ Q为您自动转义它们:
m(\Q/usr/local/perl*);
-
If you want a smaller section of your pattern to be only literal characters, you can use
\Q
then turn it off with\E
:如果您希望模式的较小部分仅为文字字符,则可以使用\ Q然后使用\ E将其关闭:
m(/usr/local/perl\Q*+?\E/);
-
The
\Q
is really the same thing as quotemeta. Putting your pattern into a variable then interpolating it in the match operator also solves the delimiter problem:\ Q与quotemeta真的是一样的。将模式放入变量然后在匹配运算符中进行插值也可以解决分隔符问题:
my $pattern = quotemeta( '/usr/local/perl*+?/' ); m/$pattern/;
#1
8
Both characters have special meaning in Perl regular expression, so you have to escape them with a backslash:
这两个字符在Perl正则表达式中都有特殊含义,因此您必须使用反斜杠转义它们:
$val =~ /\/\*/
Btw, you should probably add ^
in front of all the regexes, because you seem to want to handle only text on the beginning of the line.
顺便说一下,你应该在所有正则表达式前添加^,因为你似乎只想处理行开头的文本。
#2
16
The *
is a special character. So, you have to escape it:
*是一个特殊字符。所以,你必须逃脱它:
m{/\*}
As Adam Bellaire has suggested, here is a brief explanation:
正如Adam Bellaire建议的那样,这是一个简短的解释:
Picket-fences are best avoided. For that, sometimes, delimiters other than /
have to be used. m
should precede the first delimiter when using such delimiters. If any of the brackets are used as the first delimiter, the corresponding closing bracket has to be used as the end delimiter.
最好避免使用栅栏。为此,有时候,除了/必须使用分隔符。使用此类分隔符时,m应位于第一个分隔符之前。如果任何括号用作第一个分隔符,则必须使用相应的闭合括号作为结束分隔符。
#3
12
There are various ways to un-meta regex metacharacters. In your case, you need to handle the character that is the default delimiter as well as a meta-character.
un-meta正则表达式元字符有多种方法。在您的情况下,您需要处理作为默认分隔符的字符以及元字符。
-
In the case of a delimiter character that you want to be a literal character, you can escape that character with \, although you can get leaning toothpick syndrome:
对于你想成为文字字符的分隔符,你可以用\来逃避那个角色,虽然你可以得到倾斜的牙签综合症:
m/\/usr\/local\/perls/;
-
You can change the delimiter:
您可以更改分隔符:
m(/usr/local/perl);
-
You can escape meta-characters in the same way:
您可以以相同的方式转义元字符:
m(/usr/local/perl\*);
-
If you want a section of your pattern to be only literal characters, you can use
\Q
to automatically escape them for you:如果您希望模式的某个部分只是文字字符,则可以使用\ Q为您自动转义它们:
m(\Q/usr/local/perl*);
-
If you want a smaller section of your pattern to be only literal characters, you can use
\Q
then turn it off with\E
:如果您希望模式的较小部分仅为文字字符,则可以使用\ Q然后使用\ E将其关闭:
m(/usr/local/perl\Q*+?\E/);
-
The
\Q
is really the same thing as quotemeta. Putting your pattern into a variable then interpolating it in the match operator also solves the delimiter problem:\ Q与quotemeta真的是一样的。将模式放入变量然后在匹配运算符中进行插值也可以解决分隔符问题:
my $pattern = quotemeta( '/usr/local/perl*+?/' ); m/$pattern/;