• Login
Community
  • Login

How to find and replace the last occurrence of a tag ?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 2 Posters 243 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D
    dr ramaanand
    last edited by dr ramaanand Nov 15, 2024, 4:56 AM Nov 15, 2024, 4:54 AM

    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

    D 1 Reply Last reply Nov 15, 2024, 8:11 AM Reply Quote 0
    • D
      dr ramaanand @dr ramaanand
      last edited by Nov 15, 2024, 8:11 AM

      I think this page should have been titled, “How to find and replace the last occurrence of a string ?”

      1 Reply Last reply Reply Quote 0
      • G
        guy038
        last edited by guy038 Nov 16, 2024, 12:13 PM Nov 16, 2024, 12:05 PM

        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 string

        However, 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 tag

        Note 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

        D 1 Reply Last reply Nov 16, 2024, 5:11 PM Reply Quote 0
        • D
          dr ramaanand @guy038
          last edited by Nov 16, 2024, 5:11 PM

          @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(?: .?)?>

          1 Reply Last reply Reply Quote 0
          • G
            guy038
            last edited by guy038 Nov 17, 2024, 9:01 AM Nov 16, 2024, 6:57 PM

            Hello, @dr-ramaanand and All,

            I can only repeat what I said earlier: Yes, that’s exactly the only way : (?s)\A.*?\KTAG 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, named TAG_1, my generic regex becomes :

            (?s-i)\A.*?<TAG_1(?: .*?)?>.*?\K<TAG_2(?: .*?)?>

            BR

            guy038

            D 1 Reply Last reply Nov 17, 2024, 10:57 AM Reply Quote 0
            • D
              dr ramaanand @guy038
              last edited by Nov 17, 2024, 10:57 AM

              @guy038 Oui, merci beaucoup!

              1 Reply Last reply Reply Quote 0
              3 out of 6
              • First post
                3/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors