in reply to nested reg ex over multiple lines

Maybe you're reading the data in wrong somehow? The following seems to do what you want, I think. (Not 100% sure if I followed you, but hope this helps.)
use strict; use warnings; my $content = ""; while (<DATA>) { $content = $content . $_; } print "content: $content"; # sanity check while ($content =~m/^(CAL.+\((\w+)\))/mg){ print "\n1= $1"; print "\n2= $2"; } __DATA__ CALCON(test1) { TYPE(U8) FEATURE(DCOM) NAM(stmin) LABEL(Min seperation time between CFs) MIN(0) MAX(127) UNITS(ms) } CALCON(test2) { TYPE(U16) FEATURE(DCOM) NAM(dcomc_sestmr_timeout) LABEL(DCOM Session Timer Timeout) MIN(0) MAX(65535) UNITS(ms) }
This outputs:
1= CALCON(test1) 2= test1 1= CALCON(test2) 2= test2

Replies are listed 'Best First'.
Re^2: nested reg ex over multiple lines
by eg8rds (Acolyte) on Jun 20, 2005 at 13:30 UTC
    I have the entire file in one variable. I can get the result you have by leaving the ~m//mg rather than ~m//smg (single line mode.) My problem is that I want $1 to equal the entire element, i.e.
    CALCON(test2) { TYPE(U16) FEATURE(DCOM) NAM(dcomc_sestmr_timeout) LABEL(DCOM Session Timer Timeout) MIN(0) MAX(65535) UNITS(ms) }
    but $2 to equal "test2". perl seems to be greedy, and gets all the way to the "ms" in brackets if I add the /s to the match, i.e. it is greedy. Does this explain things better? Cheers.
      I have the entire file in one variable.
      Maybe you should have mentioned that. Anyway here's a working solution:
      use strict; use warnings; $_ = qq"CALCON(test1) { TYPE(U8) FEATURE(DCOM) NAM(stmin) LABEL(Min seperation time between CFs) MIN(0) MAX(127) UNITS(ms) } CALCON(test2) { TYPE(U16) FEATURE(DCOM) NAM(dcomc_sestmr_timeout) LABEL(DCOM Session Timer Timeout) MIN(0) MAX(65535) UNITS(ms) }"; while ( /(CALCON\(\w+\)\n{\n[^}]+})/msg ) { print "****\n$1\n"; }
      Output:
      **** CALCON(test1) { TYPE(U8) FEATURE(DCOM) NAM(stmin) LABEL(Min seperation time between CFs) MIN(0) MAX(127) UNITS(ms) } **** CALCON(test2) { TYPE(U16) FEATURE(DCOM) NAM(dcomc_sestmr_timeout) LABEL(DCOM Session Timer Timeout) MIN(0) MAX(65535) UNITS(ms) }


      holli, /regexed monk/
      I came up with something that I think does what you want below using "inch along with negative lookahead" strategy.