I got an answer to my question here: How do I know if a file is tab or space delimited in Perl?
我在这里得到了一个问题的答案:我如何知道文件是否是Perl中的制表符或空格分隔符?
but it would really help me if someone could break down the regex and explain what is going on and why it wont work on the last line of the file.
但如果有人可以打破正则表达式并解释发生了什么以及为什么它不能在文件的最后一行上工作,那真的会对我有所帮助。
~/^(\d+\s+)+?$/
I thought the above had '+' in theback so if i add '*' it will work because * means zero or more...but that did not work
我认为上面的'+'在后面,所以如果我添加'*'它会起作用,因为*意味着零或更多...但是这不起作用
2 个解决方案
#1
Regex: /^(\d+\s+)+?$/
Parts: 1 2 3 456
- Match from the start of a line
- Find one or more numbers
- Followed by one or more spaces (or tabs)
- Find one or more of 2 and 3
- But don't be greedy in that match (that is, stop when you can, don't keep going until you can't)
- Match the end of a line.
从一行开始匹配
找一个或多个号码
后跟一个或多个空格(或制表符)
找到2和3中的一个或多个
但是不要在那场比赛中贪婪(也就是说,你可以停下来,直到你不能继续前进)
匹配一行的结尾。
It should match a string of an entire line of space or tab separated numbers. I'm not exactly sure about why it'd be failing on the last line.. perhaps there's no space character at the end? Since each number must be followed by at least one space, that might be it.
它应匹配整行空格或制表符分隔数字的字符串。我不确定为什么它会在最后一行失败..也许最后没有空格?由于每个数字后面必须至少有一个空格,可能就是这个空格。
#2
In fact the original regex does not accept empty lines, that's probably why it does not work on the last line of your file (a link to an example file would be nice). It should look like this:
事实上,原始的正则表达式不接受空行,这可能是它无法在文件的最后一行工作的原因(指向示例文件的链接会很好)。它应该如下所示:
perl -ne 'if ($_=~/^(\d+\s+)*$/){print "yep\n";}'
Another way might be to just check for all the characters to be either whitespace or a digit.
另一种方法可能是检查所有字符是空格还是数字。
When accepting empty lines:
接受空行时:
perl -ne 'if ($_=~/^[\s\d]*$/){print "yep\n";}'
When not accepting empty lines:
不接受空行时:
perl -ne 'if ($_=~/^[\s\d]+$/){print "yep\n";}'
#1
Regex: /^(\d+\s+)+?$/
Parts: 1 2 3 456
- Match from the start of a line
- Find one or more numbers
- Followed by one or more spaces (or tabs)
- Find one or more of 2 and 3
- But don't be greedy in that match (that is, stop when you can, don't keep going until you can't)
- Match the end of a line.
从一行开始匹配
找一个或多个号码
后跟一个或多个空格(或制表符)
找到2和3中的一个或多个
但是不要在那场比赛中贪婪(也就是说,你可以停下来,直到你不能继续前进)
匹配一行的结尾。
It should match a string of an entire line of space or tab separated numbers. I'm not exactly sure about why it'd be failing on the last line.. perhaps there's no space character at the end? Since each number must be followed by at least one space, that might be it.
它应匹配整行空格或制表符分隔数字的字符串。我不确定为什么它会在最后一行失败..也许最后没有空格?由于每个数字后面必须至少有一个空格,可能就是这个空格。
#2
In fact the original regex does not accept empty lines, that's probably why it does not work on the last line of your file (a link to an example file would be nice). It should look like this:
事实上,原始的正则表达式不接受空行,这可能是它无法在文件的最后一行工作的原因(指向示例文件的链接会很好)。它应该如下所示:
perl -ne 'if ($_=~/^(\d+\s+)*$/){print "yep\n";}'
Another way might be to just check for all the characters to be either whitespace or a digit.
另一种方法可能是检查所有字符是空格还是数字。
When accepting empty lines:
接受空行时:
perl -ne 'if ($_=~/^[\s\d]*$/){print "yep\n";}'
When not accepting empty lines:
不接受空行时:
perl -ne 'if ($_=~/^[\s\d]+$/){print "yep\n";}'