Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Java regex - replace all 3-digit hex color codes in html string with 6 digit ones

  • 03-09-2012 1:23pm
    #1
    Registered Users Posts: 839 ✭✭✭


    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:
    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 :D

    Thanks,
    Brian


Comments

  • Registered Users Posts: 437 ✭✭t1mm


    The (messy) way I'd do it myself:
    //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


  • Registered Users Posts: 839 ✭✭✭Dr Pepper


    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).
    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!


  • Registered Users Posts: 437 ✭✭t1mm


    Much cleaner - nicely done :)


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Should the last range not be A-F instead of A-f?


  • Registered Users Posts: 839 ✭✭✭Dr Pepper


    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.


  • Advertisement
  • Closed Accounts Posts: 8,016 ✭✭✭CreepingDeath


    FYI: There's an excellent RegEx utility here.

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

    Linky


  • Registered Users Posts: 19,018 ✭✭✭✭murphaph


    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.


Advertisement