Martin
Martin

Reputation: 362

replacing image path with regular expression

I have massive html code, with loooads of images, problem is, every single image has a different path, example:

<img src="../media/2010/01/something.jpg" />

<img src="../media/logo.png" />

What I wanted to do with regular expressions is, to find every image path and replace it with:

<img src="../img/FILENAME.EXTENSION" />

I know that it's definately possible with regular expressions ... but it's just not my cup of tea, could any1 help me please?

Cheers, Mart

Upvotes: 0

Views: 4001

Answers (3)

AabinGunz
AabinGunz

Reputation: 12347

Try this link

Using this regex <img src="[\w/\.]+"(\s|)/> and replacing with <img src="../img/FILENAME.EXTENSION" />

Upvotes: 0

Erik Larsson
Erik Larsson

Reputation: 2709

If you want to parse html, its much better if you use an html parser instead of regex. There are quite alot of them and they do a very good work.

Html Agility Pack is a good one

Upvotes: 0

Marcus
Marcus

Reputation: 12586

This might not be the best solution but it might work:

(<img.*?src=")([^"]*?(\/[^/]*\.[^"]+))

and then you use capture group 1 and 3 to create the new string (depending on flavor):

$1../img$3

You can see it in action here: http://regexr.com?2v8ir

Upvotes: 3

Related Questions