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

Hi,
I'm trying to do something like this:
[table] |+ first column |+ second column |= | content | something else |= | more content | and some more [/table]

Now, I guess I could just do:
m/\[table\].*\[\/table\]/

...but would that work with it being on newlines? Also , there can be loads of tables in the same content - so just trying to think of the best way to do this :)

TIA

Andy

Replies are listed 'Best First'.
Re: [table] regex
by ikegami (Patriarch) on Aug 18, 2009 at 16:33 UTC

    /./ doesn't match newlines unless you use the "s" modifier.

    /\[table\].*\[\/table\]/s

    You'll find this will give you problems if you have more than one set of table tags in the input, since it'll match the first opening tag and the last closing tag. Change .* to .*? to do minimal matching.

    /\[table\].*?\[\/table\]/s

    This approach won't work if you can have nested tables.

      Thanks, that works a treat :)

      $code =~ s{\[table\](.*?)\[\/table\]}{process_table($1)}ges; sub process_table { my $in = $_[0]; print qq|fgot: $in \n|; }


      Thanks again!

      Andy
Re: [table] regex
by Anonymous Monk on Aug 19, 2009 at 07:09 UTC
    YAPE::Regex::Explain
    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr/\[table\].*\[\/table\]/ )->explain; __END__ The regular expression: (?-imsx:\[table\].*\[/table\]) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- table 'table' ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- /table '/table' ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------