Duncan's blog

September 11, 2012

Regex to find 6 repeating characters

I wanted to check our codebase for where we had six repeating characters in HTML and CSS for colours, e.g. #FFFFFF for white.  Here’s the regular expression for it, basically blogged here so I can remember where to find it later:


So [a-fA-F0-9] looks for a single character that is one of the letters A – F (only need to go up to F because we’re dealing with hexadecimal numbers), or the digits 0 – 9.

The ( ) parentheses around that turns it into a backreference.

The \1 then refers to that backreferenced matched, and the {5} says to match it exactly 5 times.

So it’ll find where there’s a single matching character that is then repeated five times.

What I was doing this for was to replace all six character codes (that repeat) for colours with three character codes (because #FFF is equivalent to #FFFFFF). So to then replace it, I used the following (in Eclipse):

Find: ([a-fA-F0-9])\1{5}
Replace with: \1\1\1

We can also modify the regular expression so it also catches colours like #FF0000 or #33FF99, where we have three repeating sets of identical digits. These can also be shortened, to #F00 or #3F9.

This modified regular expression will cover this too. In this case we’re looking three times for a single character that gets repeated once each time. We then replace with just the three different single characters, ignore the repeating characters:

Find: ([a-fA-F0-9])\1{1}([a-fA-F0-9])\1{1}([a-fA-F0-9])\1{1}
Replace with: \1\2\3

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: