如何检测Perl中当前行中是否有(=)符号?

Nan*_* HE 2 perl

如何检测当前行中是否有(=)符号?谢谢.

$_ = $currentLine;
if (Include =)
{
# do some thing
}
else
{
# do another thing
}
Run Code Online (Sandbox Code Playgroud)

Axe*_*man 11

最简单的方法是使用索引:

if ( index( $line, '=' ) > -1 ) {
Run Code Online (Sandbox Code Playgroud)

它比正则表达式更快,因为它是在C级完成的,没有任何编译.如果你正在查看Perl代码,你可能不在乎注释行上是否有等号,因此有这样的:

$line =~ m/^[^#]*=/;
Run Code Online (Sandbox Code Playgroud)

如果这不符合您的需求,请使用第一个.


Ano*_*ous 7

local $_ = $currentLine;
if (/=/) {
Run Code Online (Sandbox Code Playgroud)

要么

if ($currentLine =~ /=/) {
Run Code Online (Sandbox Code Playgroud)


Eth*_*her 7

 my $currentLine; # presumably this has a value from something earlier

if ($currentLine =~ /=/)
{
    # line has an = in it
}
else
{
    # it doesn't
}
Run Code Online (Sandbox Code Playgroud)

阅读有关=~运营商在参阅perldoc perlop中和正则表达式时的perldoc perlre.