2016-05-06 2 views
2

Ich bin nicht sehr gut mit regulärem Ausdruck, also wollte ich, dass Sie mir mit einem Ausdruck helfen, nur Inhalt zu ersetzen, der zwischen den Anführungszeichen im src Attribut der hTML-Tag, dh der Inhalt dieses Attribut etwas wie folgt aus:Regulärer Ausdruck für ersetzen Inhalt von SRC-Inhalt in <IMG> html-Tag

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

in anderen Worten: src = "old content "=> src =" new content"

ich diesen Ausdruck oben in einer Frage gesehen hatte über das gleiche Thema in C#, aber nicht auf Delphi arbeiten.

Also, wie geht das?

Danke im Voraus.

+0

[* RegEx ersetzen IMG src attribu tes *] (http://stackoverflow.com/questions/14317027/regex-replacing-img-src-attributes) –

+0

@ WiktorStribiżew, enthält diese Lösung auch Tag. Ich brauche nur Inhalte von src atribute. –

+0

Verwenden Sie also Rückverweise. –

Antwort

1

LÖSUNG:

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; 
3

Regex:

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

Substitution:

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

Sie können wie etwas verwenden:

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; 

Regex Erläuterung:

<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

+0

Leider nicht für mich gearbeitet. –