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,


In reply to Re: Regex match: Ignoring first occurrences by Athanasius
in thread Regex match: Ignoring first occurences by cryion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.