in reply to matching characters and numbers with regex

james28909:

Changing the data to a character representation of the hex codes actually makes your task harder (as Athanasius indicated previously). If you have the data as a byte string, you can find the repeats like this:

while ($w =~ /((.)(\2{7}|\2{3}|\2))/sg) { ... do something ... }

Since you wanted only repeats of certain lengths, the regex is a little goofy. I had to put the sequences in descending order by length, otherwise it would just give the shortest version of the sequence.)

I played around with it a little and came up with an example:

#!/usr/bin/perl use strict; use warnings; my $t = pack "H*", '0a0a0a0a0b0b0a0a0c0c0c0c0c0c0c0c' . '1f1f2b2b2b2b3e3e7b7b7b7b7b7b7b7b' . '8f8f8f8f8f8f8f8f6c6c4b4b4b4b3f3f' . '9d9d0f0f0f0f0f0f0f0f3a3a2e2e2e2e'; repeats($t); repeats('abbcccddddeeeeeffffffggggggghhhhhhhhiiiiiiiii'); sub repeats { my $w = shift; print "\nBYTES: ", unpack("H*",$w),"\n"; while ($w =~ /((.)(\2{7}|\2{3}|\2))/sg) { my $bytes = $1; my $hex = unpack "H*", $bytes; $bytes =~ s/[\x00-\x1f\x80-\xff]/_/g; print "repeat: $hex ($bytes) pos:", pos($w)-length($bytes), "\ +n"; } }

...roboticus

When your only tool is a hammer, all problems look like your thumb.