in reply to Regex with lookahead

Thanks for the replies. I think I am making progress. For background I am actually working on a C++ project using a library that has a Perl-compliant Regex class.
I thought I'd verify my expression was indeed Perl correct.
The perl 5.10 install I have here does not include Data:Dump. We have Data::Dumper but I don't know if it's comparable. But anyway I can't fully run the solutions as written.
But I have updated my C++ code to reflect the feedback.
But it does not handle "RepeatingGroup = LEI, out, Flatten". I get no match at all. But I did not use
(?=.*(?<Flatten>\bFlatten\b))?
I used
(?:\\s*,\\s*(?P<Flatten>\\bFlatten\\b))?
I may not be understanding lookahead syntax but it seems like the ".*" would match anything
I want to make sure spaces and commas only preced the match.

Replies are listed 'Best First'.
Re^2: Regex with lookahead
by tybalt89 (Monsignor) on Aug 04, 2017 at 20:56 UTC

    Yep, Data::Dumper also works, it's just not as "cool".

    Here's an alternate that only allows Flatten, in, or out (but note that multiples are allowed)

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1196747 use strict; use warnings; use Data::Dumper; while(<DATA>) { /RepeatingGroup\s*=\s* (?<GroupID>\b\w+\b) (\s*,\s* ((?<Flatten>\bFlatten\b)|(?<Direction>\b(in|out)\b)) )* \s*$/x or next; print $_, Dumper \%+; } __DATA__ RepeatingGroup = Waiver, Flatten, out RepeatingGroup = Waiver, out RepeatingGroup = Waiver, out, Flatten RepeatingGroup = Waiver , in RepeatingGroup = Waiver , Flatten RepeatingGroup = LEI, out, Flatten

      *cool* is in the eye of the beholder, worthy monk:

      $ corelist Data::Dumper Data for 2017-01-14 Data::Dumper was first released with perl 5.005 $ corelist Data::Dump Data for 2017-01-14 Data::Dump was not in CORE (or so I think)

      To some, "cool" is working without non-core modules, not necessarily what's newer or more fashionable. I'm not saying that Data::Dump is bad, I'm just pointing out that Data::Dumper is available with almost any Perl 5 install.

        IIRC Data::Dump RFC:SHOULD be preferred because it is less exploitable security-wise. I did some searching—the similar names and stemming make it difficult—and could not find a source for my belief though, so take it with a grain of salt. I certainly prefer the default output formatting in any case. :P