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
[* RegEx ersetzen IMG src attribu tes *] (http://stackoverflow.com/questions/14317027/regex-replacing-img-src-attributes) –
@ WiktorStribiżew, enthält diese Lösung auch Tag. Ich brauche nur Inhalte von src atribute. –
Verwenden Sie also Rückverweise. –