Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    Help wanted · · · – – – · · ·
    2
    6
    3380
    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 Schrandt
      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 Frank 1 Reply Last reply Reply Quote 0
      • Claudia Frank
        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 Schrandt
          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 Frank
            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 Schrandt
              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 Frank
                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
                • First post
                  Last post
                Copyright © 2014 NodeBB Forums | Contributors