正则表达式问题 - 搜索这个或那个?

时间:2011-02-20 06:54:11

标签: regex

我想在文件中搜索行开头的字符,或空行或空行。因此,例如,假设行char的结尾是LF = chr(10)

在下面的文字中,我希望结果的计数等于(5)。所有行以字符串“T”开头并为空,但是为空格或空行。

=====FILE======  
This is good (count this one 1.)  
    --- one or more spaces (count this one 2.)  
Not so good  
This is also good (count this one 3.)
- intentionally blank -  (count next two 4.)
- intentionally blank -  (.5)
Can't be good  
=====END FILE===  

3 个答案:

答案 0 :(得分:3)

/^(T|\s*$)/

答案 1 :(得分:1)

要匹配上述内容:

^(T.*| *)$

答案 2 :(得分:0)

这将完成这项工作:

/^([T ].+|$)/

这是一个Perl脚本示例:

#!/usr/bin/perl
use strict;
use warnings;
use 5.12.0;

my $count;
while(<DATA>) {
    $count++ if /^([T ].+|$)/;
}
say $count;

__DATA__
This is good (count this one 1.)  

Not so good  
This is also good (count this one 3.)


Can't be good  

输出:5