0

I am parsing one input file which has following content.

   <tr>
 <th width="50%">ABC</th><th width="50%">XYZ</th>
   </tr>
   <tr>       
   <tr>
        <td>avc</td>
        <td>fds</td>
   </tr>

code:

 #!/usr/bin/perl
 open(fh,$ARGV[0]) or die "could not open a file\n";
 $input=<fh>
 #print($input)
 if($input =~ /&lt;tr&gt;(\n)?(.*)(\n)?tr&gt;/)
 { 
     print($1);
 }

But there is no output. How to get middle line having th tag?

2 Answers 2

3

How can you match text that spans multiple lines if you only read one line? Perhaps you were trying to load the entire file, which you can do as follows;

my $input; { local $/; $input = <fh>; }

By the way, always use use strict; use warnings;!

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're only reading the first line...

Why don't you put your code inside a while loop ?

(Also, getting the entiere file by setting $/ to '' is a better idea as suggested, as you're looking for a pattern matching several lines)

This code works :

 #!/usr/bin/perl
 open(fh,$ARGV[0]) or die "could not open a file\n";
 {
    local $/;
    $input=<fh>;
    if($input =~ /&lt;tr&gt;\s*(.*)\s*&lt;\/tr&gt;/)
     { 
         print($1);
     }
    }

(Notice that I removed the parenthesis around \n which were useless)

However it's not very clean...

Also, why don't you start with :

$input=~s/&lt/</g;
$input=~s/&gt/>/g;

Which will help your code being more readable ?

1 Comment

My code has multiline <tr><th></th></tr> <tr><td></td></tr> but above code parse only 'th' tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.