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

  • 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.

    <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)


  • 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>



  • 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)

  • Enjoy the Easter holidays!

    Thank you and I wsh you the same.


  • 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 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)

  • 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.


Log in to reply