G'day cryion,

Welcome to the Monastery.

"But I have no way around using regex at the moment."

Using a regex to parse XML is generally a poor choice. Why do you have no way around this?

On the basis that you must use a regex, there is a distinct disconnect between the code and data you've posted and the regex you say doesn't work.

Parsing your XML code, line by line, with the regex you've shown (i.e. 'file:(.*?).xml'), captures one piece of data:

/path/to/some/file

Had you used different paths, such that you could see which path was being matched, you'd know that 'file:/path/to/some/file.mxf' ("the very first occurence of the file: string") was not matched at all. Consider this test:

#!/usr/bin/env perl -l use strict; use warnings; my $re = qr{file:(.*?).xml}; while (<DATA>) { print $1 if /$re/; } __DATA__ <xml> <info> <file>file:/path/to/someA/file.mxf</file> </info> <info> <file>file:/path/to/someB/file.xml</file> </info> </xml>

Output:

/path/to/someB/file

So, you're matching the right path, but not capturing all of it.

A '.' in a regex matches any character (except newline), so you really need '\.xml', not '.xml'. The closing parenthesis needs to be after '\.xml' to capture to whole pathname.

Making those changes:

#!/usr/bin/env perl -l use strict; use warnings; my $re = qr{file:(.*?\.xml)}; while (<DATA>) { print $1 if /$re/; } __DATA__ <xml> <info> <file>file:/path/to/someA/file.mxf</file> </info> <info> <file>file:/path/to/someB/file.xml</file> </info> </xml>

Gives this output:

/path/to/someB/file.xml

Which is what you state you wanted: "the whole path to the xml file".

— Ken


In reply to Re: Regex match: Ignoring first occurences by kcott
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.