kbone has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to write a code to obtain certain tables (both in text and html formats) from text documents. I am having trouble obtaining the data between the 'Table' tags. Here are key pieces of my current code:

my $startstring='(<table)'; my $keywords='((Beneficially\s*Owned)|(Beneficial\s*Owners)|(Security\ +s*Ownership)|(more\s*than\s*5\spercent)|(more\s*than\s*5\%)|(more\s*t +han\s*five\s*percent)|(All\s*Directors\s*and\s*Executive\s*Officers)| +(Preferred\s*Stock))'; my $endstring='(\/table>)'; { # this step removes the default end of line character (\n) # so the the entire file can be read in at once. local $/; open (SLURP, "$direct$slash"."$file") or die "can't open $file: $!"; #read the contents into data $data = <SLURP>; } @finds=$data=~m/$keywords\s*.{0,1500}\s*($startstring.*?$endstring)/gi +sm;

The issue is is that I am obtaining matches of $keywords, etc. I want to obtain just what is inside the parentheses of my regex above. Any help would be greatly, greatly appreciated. Thanks so much.

Replies are listed 'Best First'.
Re: Matching Multiple Multiple-Line Regex
by jwkrahn (Abbot) on Feb 20, 2012 at 22:35 UTC

    It appears that you want to use non-capturing parentheses (?:pattern) in some of your patterns.

      Thanks for the help!
Re: Matching Multiple Multiple-Line Regex
by JavaFan (Canon) on Feb 20, 2012 at 23:08 UTC
    Remove all the parenthesis from $startstring, $keywords and $endstring, and write:
    @finds = $data =~ m/(?:$keywords)\s*.{0,1500}\s*$startstring(.*?)$ends +tring/gis;
    But do note that if your text is:
    Security Ownership bla bla <table>Foo bar</table> more than 5% <table>Text text</table> All Directors and Executive Officers <table>Even more text</table>
    @finds will only get one entry: "Even more text", due to the greediness of .{0,1500}.
      Thank you so much for your reply!