[Question] Java regex - replace all 3-digit hex color codes in html string with 6 digit ones - boards.ie
Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
Thread Tools Search this Thread
03-09-2012, 13:23   #1
Dr Pepper
Registered User
 
Dr Pepper's Avatar
 
Join Date: Nov 2003
Location: Dunboyne, Meath
Posts: 800
Java regex - replace all 3-digit hex color codes in html string with 6 digit ones

Hi All,

I need a regular expression to convert 3-digit hex color codes in html to 6 digit ones. I want to do this because when I put html text into swing text components, the 3-digit versions don't seem to be recognised. I know how the 3-digit codes work by the way - the 3-digit code "#ABC;" is the same as the 6-digit code "#AABBCC;". I'm looking for a regex to do that simple conversion and expand/replace all instances in a given string.

Just to show some sort of effort on my part, I think this might find 3-digit color codes:
Code:
String s = html.replaceAll("#[0-9a-fA-F]{3};","????");
I have no idea how to grab the A, B and C from the input string and put them into the output results. Of course, let me know if you know of any other entirely different way of doing this!

In case you're wondering, this is not a homework assignment. I've been programming for many years but have managed to spoof my way thus far with little or no grasp of regular expressions. They just are something I've never really gotten my head around! As a work-around, I started writing a 'simple' method to loop through the string looking for # symbols and what comes after them, yadda, yadda, yadda... but it got very large and ugly and I don't trust it. Plus, it's Monday and I'm just not up for that kind of thing

Thanks,
Brian
Dr Pepper is offline  
Advertisement
03-09-2012, 13:43   #2
t1mm
Registered User
 
Join Date: Nov 2005
Posts: 372
The (messy) way I'd do it myself:

Code:
//ArrayList of Values (can be 3 or 6 digits - 6 digits will be left alone)
ArrayList<String> list = new ArrayList<String>();
list.add("#ABC");
list.add("#123");

String char1;
String char2;
String char3;

for(int i=0; i<list.length; i++){
     if (list.get(i).length == 4){      //4 including "#"
            char1 = list.get(i).charAt(1);
            char2 = list.get(i).charAt(2);
            char3 = list.get(i).charAt(3);
            list.set(i) = "#"+char1+char1+char2+char2+char3+char3;
     }
}
This code is written off the top of my head but should give you an idea I suppose.

Cheers

Last edited by t1mm; 03-09-2012 at 13:45.
t1mm is offline  
Thanks from:
03-09-2012, 14:56   #3
Dr Pepper
Registered User
 
Dr Pepper's Avatar
 
Join Date: Nov 2003
Location: Dunboyne, Meath
Posts: 800
Thanks for the suggestion t1mm.

However, I persevered with the regex approach for a bit longer and came up with a nice one-liner solution, using some tips from this page (capturing groups from the match expression).

Code:
str.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F]);","#$1$1$2$2$3$3;");
$1 refers to the first group (in brackets), $2 refers to the second, and so on. Seems to do the job very nicely!

Last edited by Dr Pepper; 03-09-2012 at 17:04. Reason: Changes last 'f' in expression to upper-case as per Procasinator's suggestion below
Dr Pepper is offline  
(2) thanks from:
03-09-2012, 14:59   #4
t1mm
Registered User
 
Join Date: Nov 2005
Posts: 372
Much cleaner - nicely done
t1mm is offline  
03-09-2012, 17:00   #5
Procasinator
Registered User
 
Join Date: Aug 2007
Posts: 1,262
Should the last range not be A-F instead of A-f?
Procasinator is offline  
Thanks from:
Advertisement
03-09-2012, 17:03   #6
Dr Pepper
Registered User
 
Dr Pepper's Avatar
 
Join Date: Nov 2003
Location: Dunboyne, Meath
Posts: 800
Correct! Thanks, nice catch. I'd only tested HTML documents with lower-case hex codes so that could have been a headache if it stopped working again on an upper-case code.
Dr Pepper is offline  
06-09-2012, 08:57   #7
CreepingDeath
Registered User
 
CreepingDeath's Avatar
 
Join Date: Jun 2006
Posts: 4,355
FYI: There's an excellent RegEx utility here.

There's the online version, or what I much prefer, an Eclipse plugin.

Linky
CreepingDeath is offline  
Thanks from:
07-09-2012, 10:41   #8
murphaph
Registered User
 
Join Date: Apr 2004
Location: Berlin
Posts: 13,804
Quote:
Originally Posted by CreepingDeath View Post
FYI: There's an excellent RegEx utility here.

There's the online version, or what I much prefer, an Eclipse plugin.

Linky
That eclipse plugin is soooo handy the way it matches with colour coding. Very intuitive. Always install that as part of any fresh eclipse install

You can also copy the regex and simultaneously escape it for use in java and paste straight into your java code. With long regexes it can be easy to make a mistake escaping them.
murphaph is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet