-2

Want to insert opening and closing element for set of <p> tags.

input

<p>Para1</p>
<p>Para2</p>
<p>Para3</p>

expected output

 <div>
   <para>Para1</para>
   <para>Para2</para>
   <para>Para3</para>
 </div>

How to find the first and last occurrence of <p>(.[^\<]*)<\/p> and insert <div>

VSe
  • 919
  • 2
  • 13
  • 29
  • 1
    Please don't use regular expressions for this job. They are the wrong tool to use. Seriously. They're not very good at 'handling' tagged languages like XML and HTML. See: [Parsing (X)HTML with regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) Use a parser. If this is HTML, use a HTML parser. If it's XML, an XML parser. (there's some commonality, XML is stricter) – Sobrique Oct 20 '15 at 08:38
  • Agreed, the OP is explaining what he wants to do, but not why he wants to do it. We need this info first to be able to give proper advice. – klaar Oct 20 '15 at 08:48

1 Answers1

-1

Just span your regexp from the very first <p> to the very last </p> and put the <div>s around it: s#(<p>.*</p>)#<div>\n$1\n</div>#s.

edit: as @mbethke has noticed, to be able to let . span accross newlines, you must use the s-modifier.

klaar
  • 601
  • 6
  • 17
  • 2
    Don't forget the /s modifier to the regexp if your input contains newlines. Another idea: why do this by hand at all? Modules like `Mojo::DOM` exist that make this really easy and fairly safe. Simple hand-crafted regexps are usually brittle and only a good idea if you know your inputs well and speed is very important. – mbethke Oct 20 '15 at 07:35
  • I have tried to identify the first `

    ` set by making variable into 1 like `$i=1; $i = $i+1; if $i=1` then its first. But I can't find the last set. Is there any idea to find in this way?
    – VSe Oct 20 '15 at 08:40
  • I don't really understand what you mean; my regexp will span across the complete set of `

    `aragraphs in one single attempt.

    – klaar Oct 20 '15 at 08:49