How to find and replace the last occurrence of a tag ?
-
Block of text to find and replace:-
<div style="margin-bottom:-15px;width: 100%;background-color:#EBF4FB;"> <div class="left"> <div class="left">
I used
(?s)<div style="margin-bottom:-15px;width: 100%;background-color:#EBF4FB;">.*\K<div class="left">
and<div class="right">
in the replace in files field with the Regular expression mode selected -
I think this page should have been titled, “How to find and replace the last occurrence of a string ?”
-
Hello, @dr-ramaanand and All,
You said that your regex searches the last ooccurrence of the tag
<div class="left">
and suggested to change the title as 'How to find and replace the last occurrence of a stringHowever, your statement is erroneous. Actually, you just try to find the last occurrence of the
<div class="left">
tag, AFTER a first occurrence of the<div style="margin-bottom:-15px;width: 100%;background-color:#EBF4FB;">
tag !
I think that the correct way to match the last occurrence of a specific tag, in current file, is to use the generic regex :
(?s-i)\A.*\K<
TAG Name(?: .*?)?>
Just replace the generic TAG Name value with a valid
HTML
tagNote that, in case of the comment tag, replace the generic TAG Name, into the above regex, by the literal string
!--.*?--
Similarly, the correct way to match the first occurrence of a specific tag, in current file, is to use the generic regex :
(?s-i)\A.*?\K<
TAG Name(?: .*?)?>
BR
guy038
-
@guy038 so the correct way to match the first occurrence of a specific string, in the current file, is to use the generic regex :
(?s-i)\A.?\K<STRING(?: .?)?>
-
Hello, @dr-ramaanand and All,
I can only repeat what I said earlier: Yes, that’s exactly the only way :
(?s)\A.*?\K
TAG name(?:.*?)?>
!Of course, if you want to find a the first occurrence of the
TAG_2
tag AFTER the first occurrence of an another tag, namedTAG_1
, my generic regex becomes :(?s-i)\A.*?<
TAG_1(?: .*?)?>.*?\K<
TAG_2(?: .*?)?>
BR
guy038
-
@guy038 Oui, merci beaucoup!