http://qs321.pair.com?node_id=490586


in reply to Need Regex help

Try this:

#!/usr/local/bin/perl $re = qr@ \{ (?: (?> [^{}]+ ) # Non-{} without backtracking | (??{ $re }) # Group with matching {} )* \} @x; my $string = 'value:patternList = "{error 1 1 {^E 0-20-9:0-50-9:0-50-9 +.*$} {^E 0-20-9:0-50-9:0-50-9.*$}} {three 1 1 {^.*35=A.*$|^.*35=5.*$} + {^.*35=A.*$|^.*35=5.*$}} {fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*V +FIXFxProxy.*Disconnected}"'; while($string =~ m/$re/g){ print "$&\n"; } exit; __END__ output: {error 1 1 {^E 0-20-9:0-50-9:0-50-9.*$} {^E 0-20-9:0-50-9:0-50-9.*$}} {three 1 1 {^.*35=A.*$|^.*35=5.*$} {^.*35=A.*$|^.*35=5.*$}} {fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*VFIXFxProxy.*Disconnected}

See perlre


They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^2: Need Regex help
by Earindil (Beadle) on Sep 09, 2005 at 15:46 UTC
    So using this, how would he then get his five elements from the results? Seeing as how some of the elements are encased in {} if they contain spaces or |'s?
    {error 1 1 {^E 0-20-9:0-50-9:0-50-9.*$} {^E 0-20-9:0-50-9:0-50-9.*$}} {three 1 1 {^.*35=A.*$|^.*35=5.*$} {^.*35=A.*$|^.*35=5.*$}} {fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*VFIXFxProxy.*Disconnected}

      #!/usr/local/bin/perl $re = qr@ \{( (?: (?> [^{}]+ ) # Non-{} without backtracking | (??{ $re }) # Group with matching {} )* )\} @x; my $string = 'value:patternList = "{error 1 1 {^E 0-20-9:0-50-9:0-50-9 +.*$} {^E 0-20-9:0-50-9:0-50-9.*$}} {three 1 1 {^.*35=A.*$|^.*35=5.*$} + {^.*35=A.*$|^.*35=5.*$}} {fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*V +FIXFxProxy.*Disconnected}"'; my $count = 1; while($string =~ m/$re/g){ my $inst = $1; my(@elements) = ($inst =~ m/((?<={)[^}]+(?=})|[^\s{}]+)/g); print "Instance $count\'s elements = ".join("\n", @elements)." +\n\n\n"; $count++; } exit; __END__ Instance 1's elements = error 1 1 ^E 0-20-9:0-50-9:0-50-9.*$ ^E 0-20-9:0-50-9:0-50-9.*$ Instance 2's elements = three 1 1 ^.*35=A.*$|^.*35=5.*$ ^.*35=A.*$|^.*35=5.*$ Instance 3's elements = fixv 1 1 ^.*VFIXFxProxy.*Disconnected ^.*VFIXFxProxy.*Disconnected

      Update Removed unnessicary grouping paranethesis.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

Re^2: Need Regex help
by Earindil (Beadle) on Sep 09, 2005 at 15:10 UTC
    this regexp doesn't seem to be working as posted.

      What version of perl are you using? It works as posted for me running perl 5.6.1 on Redhat linux 8.

      From perlre:

      (??{ code }) WARNING: This extended regular expression feature is considered hi +ghly experimental, and may be changed or deleted without notice. A si +mplified version of the syntax may be introduced for commonly used id +ioms. This is a ``postponed'' regular subexpression. The code is evaluat +ed at run time, at the moment this subexpression may match. The resul +t of evaluation is considered as a regular expression and matched as +if it were inserted instead of this construct. The code is not interpolated. As before, the rules to determine wh +ere the code ends are currently somewhat convoluted. The following pattern matches a parenthesized group: $re = qr{ \( (?: (?> [^()]+ ) # Non-parens without backtracking | (??{ $re }) # Group with matching parens )* \) }x;

      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        that was it. default perl on the box is 5.004, switched to use 5.6 and worked fine.