Re: parsing a line



On 6/28/07, alok nath <aloknathlight@xxxxxxxxx> wrote:
snip
if( $_ =~ m/ID\s=\s"(.*?)"\sDirAbsolute/){
snip

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";
}
.



Relevant Pages

  • Re: parsing a line
    ... Subject: parsing a line ... It does look fragile. ... A lot depends on how likely the real input ...
    (perl.beginners)
  • Re: processing large datafiles
    ... These are not equivalent statements (the regex is not anchored). ... Regexes can be faster than string comparisons*. ... regex => sub { ...
    (perl.beginners)
  • Re: regex help
    ... trying to debug a long regex in situ can be a nightmare however ... Use the interactive interpreter and see if half the regex works, ... which it goes wrong in a snip. ... especially when there are very good XML parsing libraries for Python ...
    (comp.lang.python)
  • Re: Paths, Spaces, Getopt::Long
    ... My intent is to keep it within Perl, but I seem to be going further outside of it due to this problem. ... I'm thinking that a little RegEx might just serve me better even though it's reinventing the wheel. ...
    (perl.beginners)
  • Re: [scoring] Beginnings of a generalised scoring FAQ
    ... on the assumption that threads derived from my posts ... 'This article does not match any scorefile entries...). ... I'm not competent to write a complete regex tutorial. ...
    (news.software.readers)