develooper Front page | perl.beginners | Postings from June 2007

Re: parsing a line

Thread Previous | Thread Next
Chas Owens
June 28, 2007 07:16
Re: parsing a line
Message ID:
On 6/28/07, alok nath <> wrote:
> if( $_ =~ m/ID\s=\s"(.*?)"\sDirAbsolute/){

It does look fragile.  A lot depends on how likely the real input
matches the example you gave.  That regex will break if the input is

<Test Description = "Test 1" ID =  "ID A1" DirAbsolute = "C:/perl"/>

Note the second space after the "ID =".  Also, you can generalize the
code by using character classes:

if (my %rec = $s =~ /\s*([\w ]*\w)\s*=\s*"(.*?)"/g) {
        my $id   = exists $rec{ID} ? $rec{ID} : "not set";
        my $dir  = exists $rec{DirAbsolute} ? $rec{DirAbsolute} : "not set";
        my $desc = exists $rec{'Test Description'} ? $rec{'Test
Description'} : "not set";
        print "id $id dir $dir desc $desc\n";

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About