0

I'm trying to parse parts of an SVG file to get HEX fill colors (like #ceff80 etc)

When I do it with Bash grep command:

grep -o -e "fill:#[a-f, 0-9]*;" Sample.svg

Results are as follows:

fill:#000000;
fill:#ff0000;
fill:#ff9955;
fill:#ff9955;
fill:#ffffff;
fill:#ff0000;
fill:#800080;
fill:#666666;
fill:#666666;
fill:#00ff00;

But when I try it with Python's re module, I get None:

import re
color = re.match(r'fill:#[a-f, 0-9]*;', style)

This is invoked inside a loop that walks through the XML structure using ElementTree. The style string contains values like this:

font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;display:inline;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;filter:url(#filter5107)

I want to extract the fill:#xxxxxx; color to then truncate it with [6:-1] range expression for further processing.

For some reason it always returns a None type object.

What am I doing wrong?

unfa
  • 463
  • 6
  • 13

1 Answers1

1

You should use re.search() instead of re.match().

re.match() only checks if the string exactly matches the RegExp, so if there's anything in the beginning that is unexpected, it's going to fail.

unfa
  • 463
  • 6
  • 13