1

i have a string like this:

font-size:36pt;color:#ffffff;background-color:#ff0000;font-family:Times New Roman;

How can I get the value of the color and the value of background-color?

color:#ffffff;

background-color:#ff0000;

i have tried the following code but the result is not my expected.

Pattern pattern = Pattern.compile("^.*(color:|background-color:).*;$");

The result will display:

font-size:36pt; color:#ffffff; background-color:#ff0000; font-family:Times New Roman;
3
  • 1
    What did you try? Commented Jun 1, 2018 at 6:48
  • 2
    but seems not work - pretty vague, what did you do with the pattern after this? Please add a minimal, complete, and verifiable example Commented Jun 1, 2018 at 6:53
  • Maybe string.split(";").filter(s -> s.contains("color") ? Commented Jun 1, 2018 at 6:55

5 Answers 5

3

If you want to have multiple matches in a string, don't assert ^ and $ because if those matches, then the whole string matches, which means that you can't match it again.

Also, use a lazy quantifier like *?. This will stop matching as soon as it finds some string that matches the pattern after it.

This is the regex you should use:

(color:|background-color:)(.*?);

Group 1 is either color: or background-color:, group 2 is the color code.

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

As a side question (and I'm really trying to learn!), lazy quantifiers seem to make the regex less efficient (at least by [regex101.com](regex101.com) calculations). Do you still think it should be used?
@GalAbra I found this article about performance: blog.stevenlevithan.com/archives/greedy-lazy-performance
1

To do this you should use the (?!abc) expression in regex. This finds a match but doesn't select it. After that you can simply select the hexcode, like this:

String s = "font-size:36pt;color:#ffffff;background-color:#ff0000;font-family:Times New Roman";

Pattern pattern = Pattern.compile("(?!color:)#.{6}");
Matcher matcher = pattern.matcher(s);

while (matcher.find()) {
    System.out.println(matcher.group());
} 

1 Comment

I think, also the color part should be a group - then the order of color/background-color in the input does not matter (you can use the result to see which match it is)
1
Pattern pattern = Pattern.compile("color\\s*:\\s*([^;]+)\\s*;\\s*background-color\\s*:\\s*([^;]+)\\s*;");
Matcher matcher = pattern.matcher("font-size:36pt; color:#ffffff; background-color:#ff0000; font-family:Times New Roman;");
if (matcher.find()) {
    System.out.println("color:" + matcher.group(1));
    System.out.println("background-color:" + matcher.group(2));
}

Comments

1

No need to describe the whole input, only the relevant part(s) that you're looking to extract.

The regex color:(#[\\w\\d]+); does the trick for me:

String input = "font-size:36pt;color:#ffffff;background-color:#ff0000;font-family:Times New Roman;";
String regex = "color:(#[\\w\\d]+);";
Matcher m = Pattern.compile(regex).matcher(input);
while (m.find()) {
    System.out.println(m.group(1));
}

Notice that m.group(1) returns the matching group which is inside the parenthesis in the regex. So the regex actually matches the whole color:#ffffff; and color:#ff0000; parts, but the print only handles the number itself.

Comments

1

Use a CSS parser like ph-css

String input = "font-size:36pt; color:#ffffff; background-color:#ff0000; font-family:Times New Roman;";
final CSSDeclarationList cssPropertyList = 
    CSSReaderDeclarationList.readFromString(input, ECSSVersion.CSS30);
    System.out.println(cssPropertyList.get(1).getProperty() + " , "
            + cssPropertyList.get(1).getExpressionAsCSSString());
    System.out.println(cssPropertyList.get(2).getProperty() + " , "
            + cssPropertyList.get(2).getExpressionAsCSSString());

Prints:

color , #ffffff
background-color , #ff0000

Find more about ph-css on github

Comments

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.