Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 2 Posters 4.2k Views 2 Watching
    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.
    • Peter SchrandtP Offline
      Peter Schrandt
      last edited by

      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 Reply Last reply Reply Quote 0
      • Claudia FrankC Offline
        Claudia Frank @Peter Schrandt
        last edited by

        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 Reply Last reply Reply Quote 0
        • Peter SchrandtP Offline
          Peter Schrandt
          last edited by

          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 Reply Last reply Reply Quote 0
          • Claudia FrankC Offline
            Claudia Frank
            last edited by

            Enjoy the Easter holidays!

            Thank you and I wsh you the same.

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 0
            • Peter SchrandtP Offline
              Peter Schrandt
              last edited by

              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 Reply Last reply Reply Quote 0
              • Claudia FrankC Offline
                Claudia Frank
                last edited by

                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 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors