http://www.perlmonks.org?node_id=168391


in reply to Two-dimensional match/regex?

I'm sure I must be missing the problem at hand to a certain extent, but have you looked at the m option for regular expression matches which allows for multi-line matches. For example, the regular expression match for the sample match given may look something like the following - \/\^\\\n.+C - to match:

  • the character sequence /^\,
  • a new-line character (\n),
  • one or more characters (.+), and,
  • the character, C.

This regular expression can be used in conjunction with the multi-line match option for regular expressions as follows, similar to how one would use the case-insensitive option for regular expressions:

if ($target =~ /\/\^\\\n.+C/m) { }

For further information see perlman:perlre

 

Update - This regular expression has been tested and does work. The effectiveness of a multi-line regular expression match can be tailored within the regular expression itself - That is, the more specific which you make your regular expression, the greater the specificity of the match, but as with all targeted solutions, there is a trade-off between sensitivity and specificity.

 

Replies are listed 'Best First'.
Re: Re: Two-dimensional match/regex?
by osfameron (Hermit) on May 22, 2002 at 10:44 UTC
    I think the above won't work as such, because it might find false matches (e.g. where the second line isn't aligned with the first.)

    But, I could find the first line and then generate a multi-line regex with the rest of the search string, using /m as you suggest: something like   /^.{$pos}$string1.*\n^.{$pos}$string2.*/m to make it search only at the right position...
    (Apologies for dodgy regex, not tested and probably wrong).

    thanks for the tip, I should have thought of that already!

    Cheerio!
    Osfameron
    http://osfameron.perlmonk.org/chickenman/

      You could always surround the terms which lie between the stuff you want to match with (), and check that $1, $2, etc. are the same length as the first, second, etc. line matched.