My general advice is that you are doing way more in one regex than you need to do. Perl is extremely good with regex both performance and feature wise. But not every feature needs to be used for every problem.

In Perl, I would use a module like Config::Tiny to parse the .ini file. This would handle different [Sections] in the .ini file. However it appears that you only have the "root", section (no name) to deal with.

I would consider a sequence of regular expressions instead of one complex regex. Something perhaps like below. In Perl sometimes it is actually faster execution wise to run a couple of regex'es on the same variable rather one complex one. In this case, as I understand it, your INI file which will only be parsed once at the start of your program (meaning that performance really doesn't matter that much for this task). I recommend forgetting this lookahead stuff, it is not needed here. Go with simple multiple statements.

#!/usr/bin/perl use strict; use warnings; my @example = ( 'RepeatingGroup = Waiver, Flatten, out', 'RepeatingGroup = Waiver, out', 'RepeatingGroup = Waiver, out, Flatten', 'RepeatingGroup = Waiver , in', 'RepeatingGroup = Waiver , Flatten', ); foreach my $example (@example) { $example =~ s/^RepeatingGroup =\s*//; print "This is what .ini parser says: $example\n"; my ($name,$direction,$flatten) = parseRepeatingGroup($example); print " name = $name\n". " direction = $direction\n", " flatten = $flatten\n"; } sub parseRepeatingGroup { my $value_text = shift; my ($name,$rest) = $value_text =~ /^\s*(\S+)(.*)+/; my $direction = ($value_text =~ /\bin\b/i) ? 'in' : 'out'; my $flatten = ($value_text =~ /\bFLATTEN\b/i)? 1: 0; return ($name,$direction,$flatten); } __END__ This is what .ini parser says: Waiver, Flatten, out name = Waiver, direction = out flatten = 1 This is what .ini parser says: Waiver, out name = Waiver, direction = out flatten = 0 This is what .ini parser says: Waiver, out, Flatten name = Waiver, direction = out flatten = 1 This is what .ini parser says: Waiver , in name = Waiver direction = in flatten = 0 This is what .ini parser says: Waiver , Flatten name = Waiver direction = out flatten = 1
Also in Perl, I recommend that you learn about the "//=" operator which can assign a default value to a variable which is undefined. $v //='default';

In C, I would use a very different approach. I don't see the need for regex in the C code. There are other ways of doing this, but this is not a C or C++ forum.


In reply to Re: Regex with lookahead by Marshall
in thread Regex with lookahead by ericwsf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.