OK, so now I've got two problems.
I've got a list of filenames. Some of those filenames may contain a HTML hex color reference. Like #11AACC
. Also I'm interested only in image files (extensions jpg, jpeg, png, gif and bmp). So I'd like a regexp that would match if the file extension is one of the above, plus, if there is a HTML color reference anywh开发者_Python百科ere in the string, it would get captured too. If there are multiple references, I don't care which one gets captured (there should be only one), but one should get captured. More than one is OK too, I can just ignore the rest beyond the first. Oh, and also the whole thing should be case-insensitive, but that's the least of worries.
If necessary, I can restrict the position of the color reference. But then I'd prefer it to be right before the extension, because otherwise naming these files would be awkward.
I'm trying to do something like /(#[a-fA-F]{6})?\.(?:jpeg|jpg|png|gif|bmp)$/i
or /(#[a-fA-F]{6})?.*\.(?:jpeg|jpg|png|gif|bmp)$/i
, but it doesn't capture the color reference.
Is it because your character class
[a-fA-F]
which represents a hex digit is missing digits?
Try
[a-fA-F0-9]
Since you are using the i
modifier for case in-sensitiveness you can just use:
[a-f0-9]
精彩评论