in reply to Specific Regex with Multilines (/s and /m): Why Doesn't This Work?

if (/.../g) makes no sense conceptually and can lead to really weird behaviour. Get rid of those "g".

Some of your regex patterns contain newlines, yet the string against which you are matching contains at most one at the end.

Your match operators that have /m don't have "^" or "$" in the pattern, making the /m completely useless.

  • Comment on Re: Specific Regex with Multilines (/s and /m): Why Doesn't This Work?
  • Download Code

Replies are listed 'Best First'.
Re^2: Specific Regex with Multilines (/s and /m): Why Doesn't This Work?
by elvenwonder (Initiate) on Jul 19, 2011 at 14:25 UTC

    Thank you--I thought that because I was wanted all of the matches (there are several in the full text to be searched), I needed /g. But I don't because I'm pushing each into an array individually, right?

      But I don't because I'm pushing each into an array individually, right?

      Depends. As long as you can only have one match per line, then yes, the following would do:

      while (<>) { if (/.../) { push @matches, ...; } }

      If you can have multiple matches per line, you'd need something like:

      while (<>) { while (/.../g) { push @matches, ...; } }

      It's moot, however, since you want to match text that span multiple lines. You want something more like the following:

      my $file; { local $/; $file = <>; } # Slurp file. while ($file =~ /.../g) { push @matches, ...; }