在<img/> html标记

时间:2016-05-06 21:04:08

标签: regex delphi

我对正则表达式不是很好,所以我希望你帮我一个表达式来替换html标签的src属性中双引号之间的内容,即这个属性的内容就像这样:

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

换句话说:src =“old content”=&gt; SRC = “new content

我在C#中关于同一主题的问题中看到了上面这个表达式,但是在Delphi上没有用。

那么,这是怎么回事?

提前谢谢。

2 个答案:

答案 0 :(得分:3)

<强>正则表达式:

<img(.*?)src="(.*?)"(.*?)/>

<强>换人:

<img$1src="NEW VALUE"$3/>

您可以使用以下内容:

var
    ResultString: string;

ResultString := '';
try
    ResultString := TRegEx.Replace(SubjectString, '<img(.*?)src="(.*?)"(.*?)/>', '<img$1src="NEW VALUE"$3/>', [roIgnoreCase, roMultiLine]);
except
    on E: ERegularExpressionError do begin
        // Syntax error in the regular expression
    end;
end;

正则表达式说明:

<img(.*?)src="(.*?)"(.*?)/>

Options: Case insensitive; Exact spacing; Dot doesn’t match line breaks; ^$ match at line breaks; Numbered capture; Allow zero-length matches

Match the character string “<img” literally (case insensitive) «<img»
Match the regex below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is NOT a line break character (line feed, carriage return, form feed, vertical tab, next line, line separator, paragraph separator) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “src="” literally (case insensitive) «src="»
Match the regex below and capture its match into backreference number 2 «(.*?)»
   Match any single character that is NOT a line break character (line feed, carriage return, form feed, vertical tab, next line, line separator, paragraph separator) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “"” literally «"»
Match the regex below and capture its match into backreference number 3 «(.*?)»
   Match any single character that is NOT a line break character (line feed, carriage return, form feed, vertical tab, next line, line separator, paragraph separator) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “/>” literally «/>»

<img$1src="NEW VALUE"$3/>

Insert the character string “<img” literally «<img»
Insert the text that was last matched by capturing group number 1 «$1»
Insert the character string “src="NEW VALUE"” literally «src="NEW VALUE"»
Insert the text that was last matched by capturing group number 3 «$3»
Insert the character string “/>” literally «/>»

Regex101 Demo

答案 1 :(得分:1)

<强> SOLUTION:

    procedure CHANGE_IMAGES(Document: IHTMLDocument2);
    var
      I: Integer;
      HTMLImgElement: IHTMLImgElement;
      HTMLElementCollection: IHTMLElementCollection;
    begin

      HTMLElementCollection := Document.images;

      for I := 0 to HTMLElementCollection.length - 1 do
      begin
        HTMLImgElement := (HTMLElementCollection.item(I, 0) as IHTMLImgElement);
        HTMLImgElement.src := 'My_IMAGE_PATH_OR_URL';
        Exit;
      end;
    end;