Regular Expression

How can I pull out lines between two patterns that are themselves on different lines?
eg I want to extract the lines of code between <form> and </form>
from a hTML file.

I am in urgent.Please do reply
padmamvsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KennyIT Application ExecutiveCommented:
In the HTML file, try to place the <form> and </form> tags in a line by themselves. Definitely do not have 2 form tags on the same line, or else it will "break" the code.

In your PERL script:

 open (HTML, "htmlfile.htm")
 $Count=0;
 while (<HTML>)
   {
   $Line=$_;
   if ($Line =~ /<form>/i)
     {
     $Count=1;
     }
   if ($Count ne 0)
     {
     print $Line;
     # you can do whatever you want here
     }

   if ($Line =~ /<\/form>/i)
     {
     $Count=0;
     }
   

Hope this helps.
0
KennyIT Application ExecutiveCommented:
Oh yeah...if you do not want to trap the <form> and </Form> lines, then reposition the code as follows :


while (<HTML>)
   {
   $Line=$_;

   if ($Line =~ /<\/form>/i)
     {
     $Count=0;
     }

   if ($Count ne 0)
     {
     print $Line;
     # you can do whatever you want here
     }

   if ($Line =~ /<form>/i)
     {
     $Count=1;
     }

0
ozoCommented:
perl -ne 'print if /<form>/../<\/form>/' file

But if you want to parse html, and not just pull out lines between two patterns that are themselves on different lines,
then you will need something a little more clever
0
guadalupeCommented:
open (FILE, "$file");

@Info=<FILE>;

$file_lines = join(" ", @Info);

close(FILE);


$file_lines =~ /(<form>.*<\/form>)/gs;

$lines = $1;

Line will contain everything between the "forms" and including the tags.  If you don't want the tags just move the parens around the .*  like this  (.*).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.