Community
    • 登入

    regex s/r for defined pattern while keeping some of the existing text

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    6 貼文 2 Posters 3.9k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Peter SchrandtP
      Peter Schrandt
      最後由 編輯

      Hi all,

      After studying the Wiki as well as the Community forum I am still drawing a blank on my question.

      I receive loads of HTML files containing headlines marked in specific DIV containers.

      Example:
      <div class=“head2”>A headline text I need to keep</div>
      <div class=“head2”>A different headline text which needs to stay</div>
      <div class=“head2”>Even another headline we still need</div>

      I need to mark them as h2, opening with <h2> and closing with </h2> instead of the DIV tags.

      The files contain more div containers like just <div> or <div class=“some_other_classname”> which need to remain unchanged. Otherwise I’d just do a search and replace and would be done.

      I can still do that for the exclusively used opening tag, find <div class=“head2”> and replace with <h2>, but replacing </div> with </h2> would mess up all other lines where I need to keep the </div>.

      Could you please tell me how I modify the example above to

      <h2>A headline text I need to keep</h2>
      <h2>A different headline text which needs to stay</h2>
      <h2>Even another headline we still need</h2>

      ***while leaving all other lines containing DIV tags unchanged ***?

      I’d really appreciate it! :o)

      Thanks
      Peter

      Claudia FrankC 1 條回覆 最後回覆 回覆 引用 0
      • Claudia FrankC
        Claudia Frank @Peter Schrandt
        最後由 編輯

        Hello @Peter-Schrandt,

        maybe the following regex could help

        find what:<div class="head2">(.*)</div>
        replace with:<h2>\1</h2>
        

        \1 holds the text between the div tags.
        If the head2 is also different than you might use

        <div class=".*">(.*)</div>
        

        instead.

        Cheers
        Claudia

        1 條回覆 最後回覆 回覆 引用 0
        • Peter SchrandtP
          Peter Schrandt
          最後由 編輯

          Claudia, thank you so much for your instant reply and helpful advice!
          I’ll try this as soon as I am back at the worksstation, it looks exactly as it will do the trick.

          Enjoy the Easter holidays! :o)
          Peter

          1 條回覆 最後回覆 回覆 引用 0
          • Claudia FrankC
            Claudia Frank
            最後由 編輯

            Enjoy the Easter holidays!

            Thank you and I wsh you the same.

            Cheers
            Claudia

            1 條回覆 最後回覆 回覆 引用 0
            • Peter SchrandtP
              Peter Schrandt
              最後由 編輯

              I just tried it – it works perfectly fine and will save me tons of repetitive, boring and error-prone work in the future.

              With the solution you provided, I studied http://docs.notepad-plus-plus.org/index.php/Regular_Expressions again and start to understand which character does what in this combination. I would have never figured that out myself though without your help.

              Thanks again, Claudia! :o)

              1 條回覆 最後回覆 回覆 引用 0
              • Claudia FrankC
                Claudia Frank
                最後由 編輯

                Hello Peter,

                I’m not sure if the site you mentioned reflects the current state.
                I would advise to read this for searching
                and that for replacing.

                Cheers
                Claudia

                1 條回覆 最後回覆 回覆 引用 0
                • 第一個貼文
                  最後的貼文
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors