Does this line of Perl really do anything?
这一行Perl真的有什么用吗?
$variable =~ s/^(\d+)\b/$1/sg;
The only thing I can think of is that $1
or $&
might be re-used, but it is immediately followed by.
我能想到的唯一一件事是$1或$&可能会被重复使用,但是它会立即被使用。
$variable =~ s/\D//sg;
With these two lines together, is the first line meaningless and removable? It seems like it would be, but I have seen it multiple times in this old program, and wanted to make sure.
有了这两行,第一行就没有意义了吗?看起来是这样的,但是我在这个旧程序中已经见过很多次了,我想确认一下。
2 个解决方案
#1
12
$variable =~ s/^(\d+)\b/$1/sg;
- The anchor
^
at the beginning makes the/g
modifier useless. - 锚^初使/ g修饰符没用。
- The lack of the wildcard character
.
in the string makes the/s
modifier useless, since it serves to make.
also match newline. - 缺少通配符。在字符串中使/s修饰符无效,因为它用于生成。也匹配换行符。
- Since
\b
and^
are zero-width assertions, and the only things outside the capture group, this substitution will not change the variable at all. - 因为\ b和^零宽度断言,唯一捕获组外,这个替换不会改变变量。
The only thing this regex does is capture the digits into $1
, if they are found.
如果找到这些数字,regex所做的唯一事情就是将它们捕获为1美元。
The subsequent regex
随后的正则表达式
$variable =~ s/\D//sg;
Will remove all non-digits, making the variable just one long number. If one wanted to separate the first part (matched by the first regex), the only way to do so would be by accessing $1
from the first regex.
将删除所有非数字,使变量仅为一个长数字。如果想要分离第一部分(与第一个regex匹配),惟一的方法是从第一个regex访问$1。
However, the first regex in that case would be better written simply:
但是,在这种情况下,第一个regex最好简单地写为:
$variable =~ /^(\d+)\b/;
And if the capture is supposed to be used:
如果捕获物应该被使用:
my ($num) = $variable =~ /^(\d+)\b/;
#2
6
Is "taint mode" in use? (Script is invoked with -T option.)
是否使用“污染模式”?(脚本使用-T选项调用)
Maybe it's used to sanitize (i.e. untaint) user input.
也许它被用来对用户输入进行消毒(即去除污染)。
#1
12
$variable =~ s/^(\d+)\b/$1/sg;
- The anchor
^
at the beginning makes the/g
modifier useless. - 锚^初使/ g修饰符没用。
- The lack of the wildcard character
.
in the string makes the/s
modifier useless, since it serves to make.
also match newline. - 缺少通配符。在字符串中使/s修饰符无效,因为它用于生成。也匹配换行符。
- Since
\b
and^
are zero-width assertions, and the only things outside the capture group, this substitution will not change the variable at all. - 因为\ b和^零宽度断言,唯一捕获组外,这个替换不会改变变量。
The only thing this regex does is capture the digits into $1
, if they are found.
如果找到这些数字,regex所做的唯一事情就是将它们捕获为1美元。
The subsequent regex
随后的正则表达式
$variable =~ s/\D//sg;
Will remove all non-digits, making the variable just one long number. If one wanted to separate the first part (matched by the first regex), the only way to do so would be by accessing $1
from the first regex.
将删除所有非数字,使变量仅为一个长数字。如果想要分离第一部分(与第一个regex匹配),惟一的方法是从第一个regex访问$1。
However, the first regex in that case would be better written simply:
但是,在这种情况下,第一个regex最好简单地写为:
$variable =~ /^(\d+)\b/;
And if the capture is supposed to be used:
如果捕获物应该被使用:
my ($num) = $variable =~ /^(\d+)\b/;
#2
6
Is "taint mode" in use? (Script is invoked with -T option.)
是否使用“污染模式”?(脚本使用-T选项调用)
Maybe it's used to sanitize (i.e. untaint) user input.
也许它被用来对用户输入进行消毒(即去除污染)。