-2

I have a html with:

<p class="s5">Chapter 1 – General Information</p>
<p class="s5">Section 1 – Example</p>
<p>Some text</p>
<p class="s5">Chapter 2 – Introduction</p>

and I want to replace every <p class="s5"> tag that starts with Chapter for <h1> ... </h1>.

How I peform it with regex substitution in SublimeText?

Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
  • 3
    Beware. A storm is coming: http://stackoverflow.com/a/1732454/2615940 – skrrgwasme Aug 25 '15 at 22:59
  • What language are you using? @skrrgwasme it's a perfectly valid storm. – Downgoat Aug 25 '15 at 23:41
  • @vihan I wasn't trying to make any comment about the validity. I don't understand the nuances of the debate well enough to do so. I just wanted to give the OP some warning of what's to come, because storms do tend to descend upon HTML & regex questions. – skrrgwasme Aug 25 '15 at 23:44
  • I'm just correcting a PDF that was converted to HTML using SublimeText 2. – Allan Veloso Aug 26 '15 at 16:00

1 Answers1

1

You haven't indicated which language/tool you're using, so here's a generic solution:

Search: (?<=<p class="s5">)(Chapter[^<]*)
Replace: <h1>$1</h1>

Breakdown:

  • (?<=<p class="s5">) is a look behind (non-consuming assertion) for <p class="s5">
  • (Chapter[^<]*) is text starting with Chapter and everything up to the next <

If your tool doesn't understand look behinds, you can just consume and replace the preceding input instead:

Search: <p class="s5">(Chapter[^<]*)
Replace: <p class="s5"><h1>$1</h1>

Note that languages/tool vary with back-reference syntax; the $1 may need to be \1 instead.

Bohemian
  • 412,405
  • 93
  • 575
  • 722