Perl search regex only at the beginning of text
Suppose there is a text like this:
|-SAMPLE-D2
|---SAMPLE-D1
|---SAMPLE3
I want to count the number of "-" after |. I tried to parse this using the following regex in perl
$count=()= /-/g;
but this is problematic because the first two have "-" somewhere else both in the text and in the front. How do I create my own regex or use another function in perl to get the number "-" right after the "|"?
source to share
To count the dashes not preceded by a letter, use a negative appearance statement .
$count = () = /(?<!\w)-/g
source to share
If the vertical line just starts at the beginning, you can get the line of repeating minuses with:
my ($match) = $txt =~ /^\|(-*)/;
The parentheses around $ match force the captured part of the regex
then enter the number of minuses with
my $minus_count = length($match || '');
|| '')
bit
Initializes $ match if the above regex does not match, to stop the length of moaning about uninitialized variables (if you have warnings)
source to share
Not sure if you can count directly in Regex, but you can extract the capturing groups and do some simple arithmetic with their string strings:
#!/usr/bin/perl
use warnings;
my $inFile = $ARGV[0];
open(FILEHANDLE, "<", $inFile) || die("Could not open file ".$inFile);
my @fileLines = <FILEHANDLE>;
my $lineNo = 0;
my $rslt;
foreach my $line(@fileLines) {
chomp($line);
$line =~ s/^\s+//;
$line =~ s/\s+$//;
$lineNo++;
print "\n".$lineNo." = <".$line.">";
if($line =~ m/^\|-+(.+)/) {
my $text = $1;
print "\n\ttext = <".$text.">";
my $minCnt = length($line) - length($text) - 1;
print "\n\tminus count = <".$minCnt.">";
}
}
close(FILEHANDLE);
source to share