[thelist] regexp question

Matthew Fischer mfischer at e-fishsolutions.com
Thu Mar 29 11:56:45 CST 2001


> I'm trying to match repeated characters. And capture how many times they
> are repeated.
> e.g given string '--------_____' I want to know that there are 8 '-'
> characters, followed by 5 '_' characters.

I don't think you can do it with regex alone, but the following short script could be used as a subroutine to get you the info you want. 

The script puts the first character in the variable $myChar[0], then loops through the string comparing each character with the previous one. If it matches it increments the repeat counter, if not, it prints the number of repeated characters and the character repeated, then resets the repeat counter to 1.

If you changed the print statements to set a unique variable to the number of repeats and the character repeated, you could then build your regex automatically using the variables, something like:

         /$varCharacterIsSavedIn{$varNumberOfRepeatsSavedIn}/

HTH,
Matt 

<Start Script>
 
#usr/bin/perl/

$myString = "--------_____";

$repeated = 1;
$myChar[0] = substr($myString, 0, 1);

for ($x = 1; $x < length($myString); $x++) {
    $myChar[$x] = substr($myString, $x, 1);
    $compare = $x - 1;
    $myCompare = $myChar[$compare];
    if ($myCompare eq $myChar[$x]) {
        $repeated++
    } else {
    print "$repeated $myCompare\n";
    $repeated = 1;
    } 
    
}

print "$repeated $myCompare\n";
    
<End Script>

<Start Result>

8 -
5 _

<End Result>




More information about the thelist mailing list