Regex 在Perl中,如何使用正则表达式跳过包含数字的输入行?
我有一张这样的清单: Bicycles: Childrens 289 Bicycles: Mountain Bikes 928 Bicycles: Road Bikes 870 Camping & Outdoors Equipment 761 Canoes, Kayaks, Row-Boats 231 Climbing Equipment 120 Freeweights and Home Gyms 583 GPS and Locators 104 Golf Equipment 1,223 Other Fitness Equipment 668 。如果你付出一些努力,你会学到更多。如果你写作Regex 在Perl中,如何使用正则表达式跳过包含数字的输入行?,regex,perl,input,skip,Regex,Perl,Input,Skip,我有一张这样的清单: Bicycles: Childrens 289 Bicycles: Mountain Bikes 928 Bicycles: Road Bikes 870 Camping & Outdoors Equipment 761 Canoes, Kayaks, Row-Boats 231 Climbing Equipment 120 Freeweights and Home Gyms 583 GPS and Locators
while (<>) {
next unless /[^\d,\s]/;
:
}
“blank”是指带有可选行尾的空字符串(如果您不使用chomp
)下一个if(!$line | |$line!=int($line))
第一个是空白值检查,第二个是检查该值是否为整数。:-)如果($line=~/[0-9]/或$line eq”“){}请用英语解释你的意思。我在回答中写下这一行是出于一个特定的原因:1)我不知道你是否使用了
chomp
ed输入2)你使用了blank这个词来指代一行,而一行除了空格之外什么都没有,对我来说仍然是一个空白行,3)我不喜欢if
s后面的空块。如果这行不包含任何数字并且包含一些非空格字符,上面的一行会显示“print
。如果您需要其他内容,请说出来。对不起,我只是修改了您的答案以使用我的代码。这不是一个关键的脚本。它工作得很好。感谢行为OP对包含混合内容的行,例如“我是第一”。。?
print $line if $line !~ /[0-9]/ and $line =~ /\S/;
while (<>) {
next unless /[^\d,\s]/;
:
}
if ($line !~ /^[\d,]*[\n\r]*$/) {
# do something
}
else {
# blank or "digits & comas" only line
}