1

Using Regular Expressions, I am struggling to figure out how to match an image source pattern within HTML document, and replace it with a different path:

Replace source like this:

img alt="description" align=left src="/xxxx/ssss/sssss/sssss/Photos/myimage.jpg"

with like this:

img alt="description" align=left src="http://www.mysite.com/subsite/images/myimage.jpg"

keeping the same image name.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Varin
  • 119
  • 2
  • 12
  • If this is any more complicated than parsing attributes out of elements (which is, in fact, possible) you should go read this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Wug Oct 23 '12 at 20:14
  • Answer: Use a HTML parser. Reason: http://stackoverflow.com/a/1732454/1090657. – quantum Oct 23 '12 at 20:22

2 Answers2

0

Search pattern:

img alt="description" align=left src="\K[^"]*(?=")

Replace match with following value:

http://www.mysite.com/subsite/images/myimage.jpg

(Sorry, don't know C#.)

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

You may try this:

/<img\s+([^s]\w+=\"[^"]+\"\s+)*src=\"([^"]+)\"\s+(\w+=\"[^"]+\"\s+)*\/>/i

and the image src will be kept in \2, where \w means any word character (letter, number, underscore), and \s means any space character. This regex will match src even if it is not the third attribute.

You may try this on rubular.com to see how it works.

Victor Dodon
  • 1,796
  • 3
  • 18
  • 27