Chris Johnston
Chris Johnston

Reputation: 11

What regex to use?

Does anybody know the regular expression code to change any thing similar to #f2f2f2; in to #000000;. Also the one for rgb(210, 236, 238); to rgb(0, 0, 0);. I guess you can tell that in a long text file I want to change all colors to black. I am using the Notepad++ Find and Replace box(pic). Thanks

http://pctechtv.com/show/_whatregex.png

Upvotes: 0

Views: 149

Answers (3)

Matthew Estock
Matthew Estock

Reputation: 106

Match:                           Replace:
#[a-fA-F0-9]{3}[a-fA-F0-9]{3}    #000000

This regex searches for a # followed by either 3 or 6 hexadecimal characters (only valid hexadecimal values). There are more exact ways to check, but this is shorter to write.

Match:                                 Replace:
rgb\(\d{1,3},\s?\d{1,3},\s?\d{1,3}\)   rgb(0,0,0)

This regex looks for the start of a rgb color, up to a 3 digit number, separated by commas, with white-space accounted for after each comma.

Use the "Regular expression" Search Mode.

Upvotes: 1

Christian Ternus
Christian Ternus

Reputation: 8492

Find: \#([0-9]|[a-f]){6}

Replace: \#000000

Find: rgb\((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\,\s*?){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\)

Replace: rgb(0, 0, 0)

(The handy regex snippet ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) means "match a number between 0 and 255 inclusive.")

You'll want to check the "Extended" option in your dialog when you do this.

(Edit: Forgot a couple of characters in the regexes.)

Upvotes: 0

Kai Sassnowski
Kai Sassnowski

Reputation: 280

For hex: \#([0-9]|[a-f]){6}

For rgb: rgb\(([0-9]{3},[\s]?)+[0-9]{3}\);

Hope that helped.

Upvotes: 0

Related Questions