in reply to Regex match: Ignoring first occurences

Hello cryion, and welcome to the Monastery!

With the XML data shown, the regex you say doesn’t work actually does, as long as there is no /s modifier, because in the absence of that modifier . won’t match a newline character — and therefore the matching file: has to be on the same line as the \.xml.

But I’m guessing that your real data doesn’t always contain newlines as in the example. In that case, you can use a technique which I learned here at PerlMonks:

#! perl use strict; use warnings; my $xml = '<xml><info><file>file:/path1/to/some/file.mxf</file></in +fo>' . '<info><file>file:/path2/to/some/file.xml</file></info></ +xml>'; my $lmx = reverse $xml; my ($htap) = $lmx =~ /lmx\.(.*?):elif/s; if (defined $htap) { my $path = reverse $htap; print "Path: $path\n"; }

Output:

23:32 >perl 1335_SoPW.pl Path: /path2/to/some/file 23:32 >

See reverse.

Update (Aug 11, 2015):

  1. Fixed logic to prevent uninitialized warning when attempting to reverse $htap if $htap is undef.
  2. This technique will still give a false positive if, e.g., the file: immediately preceding .xml is missing or misspelled. Prefer the other solutions given above.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,