regex instead of a new lover. . .

Discussion in 'Perl Misc' started by aardvarkman, Nov 25, 2007.

  1. aardvarkman

    aardvarkman Guest

    Well, I'd rather get ahold of anusha and forget this whole business,
    but I guess I should finish this first:

    I have a config file that get read by my main script. A sub parses the
    file and does just fine under simple circumstances. It finds all
    this:

    Index directory === C:/
    HTML resource directory === C:/Aardvark/res/user_html/test_index
    HTML resources === *.html *.htm
    File types to index === html|htm|pdf|xml|zip|mif|txt

    # Frame template options:

    Template directory === C:/Aardvark/res/frame_templates
    Template types === *.fm
    Template objects === Para,Char,XRefs,Vars,Tables,Page Layouts,Math
    Defs,Ref Pages,Conditions

    I get the values on both sides of the ===.

    The problem is that when there is a . or ? (and maybe other things)
    it fails:

    # Stuff I can't read
    Title rule choices === title.fm
    TOC rule choices === TOC.fm
    Appendix rule choices === apx.fm
    Index rule choices === IOM.fm|IX.fm
    Body rule choices === Everything else!
    Other rule choices 1 === ????????
    Other rule choices 2 === ????????

    What I am looking for is a regex that finds anything one line at a
    time separated by ===. In other words, all my config options are on a
    single line like: key === value.

    I started guessing, then started researching, and now I ask the
    experts: what will work?

    Stuff I tried:

    while(<$fh>){
    if (/#########/){
    last;
    } # todo: make this match . and ?
    elsif (/^\ *(.*?)\ *===\ *(.*?)\ *$/){ # original
    # elsif (/^\ *(.*?)\ *===\ *(.*?)\ *$/sm){
    # elsif (/\A\ *(.*?)\ *===\ *(.*?)\ *\Z/sm){
    # elsif (/^ *(.*?) *=== *(.*?) *$/){
    # elsif (/^ *(.*?) *=== *(.*?) *$/sm){
    # elsif (/^\ *(.*?)\ *===\ *(.*?)$/is){
    # elsif (/^(.*?)===(.*?)$/){
    # elsif (/^\s+(.*?)\s+$===^\s+(.*?)\s+$/) {
    # elsif (/^\s+(.*?)\s+$===^\s+(.*?)\s+$/m) {
    # elsif (/^(.*?) === (.*?)$/sm) {
    $items -> {$1} = $2;
    }
    }
    return $items;
     
    aardvarkman, Nov 25, 2007
    #1
    1. Advertising

  2. aardvarkman wrote:
    > What I am looking for is a regex that finds anything one line at a
    > time separated by ===. In other words, all my config options are on a
    > single line like: key === value.


    my %config;
    while (<$fh>) {
    if ( /(.+) === (.+)/ ) {
    $config{ $1 } = $2;
    }
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 25, 2007
    #2
    1. Advertising

  3. aardvarkman

    Uri Guttman Guest

    >>>>> "GH" == Gunnar Hjalmarsson <> writes:

    GH> aardvarkman wrote:
    >> What I am looking for is a regex that finds anything one line at a
    >> time separated by ===. In other words, all my config options are on a
    >> single line like: key === value.


    GH> my %config;
    GH> while (<$fh>) {
    GH> if ( /(.+) === (.+)/ ) {
    GH> $config{ $1 } = $2;
    GH> }
    GH> }

    a 2 liner that also does the i/o (untested):
    (assuming the config come from a file)

    use File::Slurp ;
    my %config = read_file( 'config' ) =~ /^([^=]+)===(.*)$/mg ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Nov 25, 2007
    #3
  4. aardvarkman

    Guest

    Thanks! Turns out many of these regexs worked. The new ones you
    provided taught me something.

    However, the problem was in some other code. My match ended up getting
    sent to a sub that creates LabEntry widgets on the fly. So when a
    value with a non-word character went into $textvar, my validation
    string removed it from the entry field. So I was getting the correct
    stuff, but it just wasn't showing up in user interface.

    sub createLabEntry {
    my $parentwidget = $_[0];
    my $label = $_[1];
    my $textvar = $_[2];
    my $width = $_[3];

    if ( $$textvar !~ /^[\w+\|]*$/i ) { $$_textvar = ''; }
    $parentwidget->LabEntry(
    -label => $label,
    -labelPack => [ "-side" => "left" ],
    -textvariable => $textvar,
    -width => $width,
    -background => 'white',
    -validate => 'key',
    -validatecommand => sub { $_[0] =~ /^[\w+\|]*$/i },
    -invalidcommand => sub { $parentwidget->bell }
    );
    }
     
    , Nov 26, 2007
    #4
    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. Replies:
    0
    Views:
    294
  2. Replies:
    3
    Views:
    773
    Reedick, Andrew
    Jul 1, 2008
  3. brad
    Replies:
    1
    Views:
    505
    Waldemar Osuch
    Aug 5, 2008
  4. Replies:
    1
    Views:
    241
  5. Replies:
    2
    Views:
    470
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page