Skip to content

Instantly share code, notes, and snippets.

@pjf
Created July 19, 2016 23:52
Show Gist options
  • Save pjf/473bdd1875cda874e4a8c66c8ae07108 to your computer and use it in GitHub Desktop.
Save pjf/473bdd1875cda874e4a8c66c8ae07108 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie;
while (<>) {
# This requires at least two characters to fire, because we're searching
# for a character, followed by repeats ('+') of that character.
#
# We're doing string substitution (rather than building a new string),
# so if something doesn't match, we just leave it alone.
s{
(?<string> # The whole string of repeated characters.
(?<char>.) # The first character
\g{char}+ # The same character, as many times we can.
)
}{$+{char} . length($+{string} ) }exg; # Replace with character and length of string.
print;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment