Regex Question - Searching for this or that?
I want to search a file for either a character at the beginning of the line, or a blank or empty line. So for example, let's assume the end of line char is LF = chr(10)
In the text below, I want the count of the results to equal (5). Al开发者_C百科l lines beginning with the string "T" and empty but for spaces or blank lines.
=====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===
/^(T|\s*$)/
To match the above:
^(T.*| *)$
This will do the job:
/^([T ].+|$)/
here is a sample Perl script:
#!/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
output: 5
精彩评论