4

I am simply trying to get all the Hex colors values form a css file. The hex value could be #fff or #ffffff so here are the regular expressions i used for this

  • "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
  • "#([a-f0-9]{3}){1,2}/i"
  • "^#[0-9a-zA-F]{3}"

but not working at all.

i am expecting the result as

#996633 #333 #ccc #969696 ....

But getting nothing, any idea where i am going wrong?

Here is the code:

final String HEX_PATTERN_STRING = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
Pattern pattern = Pattern.compile(HEX_PATTERN_STRING);
try {
        final URL CSS = new URL("https://maxcdn.bootstrapcdn.com/.../bootstrap.min.css");
        URLConnection data = CSS.openConnection();
        StringBuilder result = new StringBuilder();
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        data.getInputStream())
        )) {
            in.lines().forEach(result::append);

            Matcher matcher = pattern.matcher(result);
            while (matcher.find()) {
                System.out.println(matcher.group(0));
            }
            System.out.println("Done");
        }

    } catch (IOException ex) {
    }
2
  • I don't see you actually associating HEX_PATTERN_STRING with your Marcher anywhere. Commented Aug 23, 2016 at 8:37
  • 2
    Try using final String HEX_PATTERN_STRING = "#(?:[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"; and add Pattern pattern = Pattern.compile(HEX_PATTERN_STRING); before the line starting with Matcher matcher. Commented Aug 23, 2016 at 8:39

2 Answers 2

2

Note your pattern contains ^ (start of string) and $ (end of string) anchors, requiring a whole string match.

You need to remove these anchors.

You cannot use regex delimiters like /.../ either, as in Java regex, you can pass the modifiers as (?i) inside the pattern, or with the help of Pattern.CASE_INSENSITIVE flag (usually, with Pattern.UNICODE_CASE).

Also, if you do not need the numbers only, you may turn the capturing group into a non-capturing (?:...).

Use

final String HEX_PATTERN_STRING = "#(?:[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})";
Sign up to request clarification or add additional context in comments.

7 Comments

my bad i just forgot to put this line pattern = Pattern.compile(HEX_PATTERN_STRING); in my code btw i am declaring paatern. i am updating the question
Do not forget to indicate your expected result and explain what "does not work" means.
I believe all you need is just to remove the anchors. I double checked: you may pass the StringBuilder to the matcher, as it is converted by Java into a string internally.
@wikitor pattern.matcher(result.toString()) is the same as pattern.matcher(result) i guess . but still i tried result.toString() and no success.
oh damm you are right ^ and $ require the whole string match i just removed them and it worked
|
0

I'm using this pattern:

Pattern PATTERN_HEXCOLOR = Pattern.compile(
    "#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\\b");

It does accept color values like

#FFF
#FFFFFF

And ignores invalid color values like

#FFFF or #FFFFFFF

5 Comments

This regex will fail here. The original question does not set any condition on the context where to find the codes, the word boundary may not be the best option to use here.
But your pattern finds also color values like #ffff. I just checked that in my HTML editor APP wich highlights all the hex color values with corresponding color
In your example there are two incorrect color codes. So my pattern seems to be correct.
#ffff is a string containing #fff color code and a letter f
Yeah, at least the Chrome Browser does not accept CSS color codes like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.