anguspcw
anguspcw

Reputation: 306

JAVA regex to find string

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;

Upvotes: 1

Views: 236

Answers (5)

jschnasse
jschnasse

Reputation: 9588

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

Upvotes: 1

GalAbra
GalAbra

Reputation: 5138

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.

Upvotes: 1

yx he
yx he

Reputation: 131

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));
}

Upvotes: 1

Sweeper
Sweeper

Reputation: 273860

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

Upvotes: 3

Corba
Corba

Reputation: 309

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());
} 

Upvotes: 1

Related Questions