help with regex

Discussion in 'Perl Misc' started by George Mpouras, Jun 19, 2013.

  1. I must discover all possible field names of a key/value file.
    The properties of the file are unknown so I must be a little creative.
    The values optional can have whitespaces inside "..."
    its key/value separated with a space from the next pair.
    Do you thing the following is ok ?



    #!/usr/bin/perl
    use strict;
    use warnings;

    while(<DATA>) { chomp;

    while ( /([^=]+)=("[^"]+"|\S+)/g ) {
    my ($key, $val) = ($1, $2);
    $val =~s/^["\s]*(.*?)["\s]*$/$1/;
    print "*$key* *$val*\n"
    }

    print "--------\n"
    }


    __DATA__
    f1=hello f2= f3="foo" f4="hello world"
    f6="day" f7="day & night" f8=100
     
    George Mpouras, Jun 19, 2013
    #1
    1. Advertising

  2. George Mpouras

    Tim McDaniel Guest

    In article <kps6f4$15u3$>,
    George Mpouras <> wrote:
    >I must discover all possible field names of a key/value file.
    >The properties of the file are unknown


    If by "properties" you mean the layout, format, et cetera,
    then how can anyone advise you on a proper way to parse it
    when neither you nor we know what's valid?

    --
    Tim McDaniel,
     
    Tim McDaniel, Jun 20, 2013
    #2
    1. Advertising

  3. George Mpouras

    Peter Gordon Guest

    George Mpouras <> wrote
    in news:kps6f4$15u3$:

    > I must discover all possible field names of a key/value file.
    > The properties of the file are unknown so I must be a little creative.
    > The values optional can have whitespaces inside "..."
    > its key/value separated with a space from the next pair.
    > Do you thing the following is ok ?
    >
    >
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > while(<DATA>) { chomp;
    >
    > while ( /([^=]+)=("[^"]+"|\S+)/g ) {
    > my ($key, $val) = ($1, $2);
    > $val =~s/^["\s]*(.*?)["\s]*$/$1/;
    > print "*$key* *$val*\n"
    > }
    >
    > print "--------\n"
    > }
    >
    >
    > __DATA__
    > f1=hello f2= f3="foo" f4="hello world"
    > f6="day" f7="day & night" f8=100
    >
    >


    You don't say if all possible sequences are included in the data.
    If they are, the code below decodes it.

    #!/usr/bin/perl -w
    use strict;
    use 5.14.0;
    my %lines;
    while( <DATA> ) {
    chomp;
    last if /^$/; # Catch blank lines at end of data.
    while ( /(f\d+)=(.*?)(?: f\d+=|$)/ ) {
    my $key = $1;
    my $value = $2;
    s/$key=$value(.*)/$1/; # Strip the key/value pair off the
    string.
    $value =~ s/"(.*)"/$1/; # Strip off any "
    $lines{$key} = $value;
    }
    }
    say "The Hash";
    foreach my $key (sort keys %lines ) {
    say "$key: $lines{$key}";
    }
    __DATA__
    f1=hello f2= f3="foo" f4="hello world"
    f6="day" f7="day & night" f8=100
     
    Peter Gordon, Jun 20, 2013
    #3
  4. key names can be whatever string with no spaces not f\d+
    f100 etc was an example, so the regex
    /(f\d+)=(.*?)(?: f\d+=|$)/ )
    is not catching correctly
     
    George Mpouras, Jun 20, 2013
    #4
  5. key names can be whatever string with no spaces not only f\d+
     
    George Mpouras, Jun 20, 2013
    #5
  6. you are correct, specs are loosy
    lines with multiple key/value pairs separated by space
    keys are not containing space
    values may contain space inside double quotes
     
    George Mpouras, Jun 20, 2013
    #6
  7. George Mpouras

    Tim McDaniel Guest

    In article <kpvome$ck5$>,
    George Mpouras <> wrote:
    >key names can be whatever string with no spaces not f\d+


    The examples were the only spec you gave, so you can understand why
    people coded to it.

    >f100 etc was an example


    An example provided by the teacher of the class?

    Settings separated by space: do you mean one space or one or more
    characters of whitespace?
    Is there always an "="? That is, you can't have "foo bar"; it must be
    "foo= bar="?
    Can there be whitespace around "=", as in "foo = bar";
    Can there be leading whitespace and/or trailing whitespace on the line?

    --
    Tim McDaniel,
     
    Tim McDaniel, Jun 20, 2013
    #7

  8. > An example provided by the teacher of the class?


    !!!!!!!!!!!!!!!!!!!!!!!!!!
     
    George Mpouras, Jun 20, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    746
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,693
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    630
  4. Xah Lee
    Replies:
    1
    Views:
    973
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    834
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page