Regular expression to replace content of the src attribute of the html img tag

3

Hello,

I do not understand almost anything of regular expression, so I wanted you to help me with an expression to replace only what I have between the double quotation marks in the src attribute of the html tag, that is, the content of this attribute something like this: / p>

TRegEx.Replace(Str, '(?<=<img\s+[^>]*?src=(?<q>[""]))(?<url>.+?)(?=\k<q>)', 'Nova string');

I had this expression of an issue talking about the same theme in C #, but in Delphi it is not working.

I look forward to any help.

    
asked by anonymous 06.05.2016 / 22:10

2 answers

1

I think the expression you are looking for is:

\<img(.|\n|\r)+src="[^"]*$1"(.|\r.|\n)+\>

Where:

  • \<img is self-explanatory;
  • (.|\n|\r) means any character. The dot means any character other than line break. The other two characters are line breaks. The pipe character ( | ) is the logical operator "or";
  • The plus sign means what's left, at least once, but until endless times;
  • [^"]* sifnifica anything other than quotation marks, from zero to infinite times;
  • $1 is a way of identifying what you want to find. I am rusty with Delphi, so if anyone knows the right way and it is not this one, please edit my answer:)
06.05.2016 / 22:30
1

From what I was seeing this will depend on the REGEX library you are using

Assuming you are using PCRE

REGEX

pattern : (<img.*src=")([^"]*)(".*>)
replace : $1"URL ALTERAÇÃO"$3

View on regex101

OBS

  • "CHANGE URL" is your string to replace group 2 which is the current url

The regex (?<=<img.*src=")([^"]*)(?=".*>) can not be used since the look behind (?<=) does not allow quantifier its composition as * .

    
10.05.2016 / 15:41