Replace src attribute in HTML body

Hi,

I need to replace all the src attributes values in a HTML body. Typically it contains values like below and I need to remove the junk value starting from ‘@’ and it should only contain till that extension followed by double quotes.
src=“cid:image002.png@01DB91BB.D6446500”
src=“cid:image001.png@01DB91BB.D6446500”
src=“cid:image007.jpg@01DB91BC.13B8F810”

Expected output:
src=“cid:image002.png”
src=“cid:image001.png”
src=“cid:image007.jpg”

I have also attached the html body in text file below for reference
second.txt (9.0 KB)
.

Hi @soorya_prasad

Use the regex cid:[^@]+ to match the src attribute, then replace it using System.Text.RegularExpressions.Regex.Replace(inputHtml, “cid:[^@]+”, “$1”) to remove the junk after the @ and keep only the file name with the extension.

Hi @soorya_prasad

Use the below expression to replace.

strText = System.Text.RegularExpressions.Regex.Replace(strText,"(?<=src.*)@[0-9A-Z.]+","")

Hope it helps!!

1 Like

string result = Regex.Replace(html_body, @“@(.*)$”, “”);

regex format = @(.*)$

Hi,

Can you try the following?

System.Text.RegularExpressions.Regex.Replace(strText,"(src=""[^@]*)@[^""]*","$1")

Sample20250310-3 (2).zip (4.5 KB)

Regards,

Thank you, It worked as expected.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.