Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 2 Posters 234 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.
    • dr ramaanandD
      dr ramaanand
      last edited by dr ramaanand

      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

      dr ramaanandD 1 Reply Last reply Reply Quote 0
      • dr ramaanandD
        dr ramaanand @dr ramaanand
        last edited by

        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
        • guy038G
          guy038
          last edited by guy038

          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

          dr ramaanandD 1 Reply Last reply Reply Quote 0
          • dr ramaanandD
            dr ramaanand @guy038
            last edited by

            @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
            • guy038G
              guy038
              last edited by guy038

              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

              dr ramaanandD 1 Reply Last reply Reply Quote 0
              • dr ramaanandD
                dr ramaanand @guy038
                last edited by

                @guy038 Oui, merci beaucoup!

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