I always try to find some sort of reliable landmark in the format I'm reading and use it to disambiguate other parts of the "markup" on a syntatictal level, rather than going into the semantics (like "does the sum of tags I have here constitue a complete DN yet?" as BrowserUk did).

In this case the (almost) reliable landmark is the commas separating the tags. If we read the file from comma to comma, there is only one ambiguous case: we can read across the newline that separates the tag at the end of one DN from that which begins the other DN. This is the one case we need to care about. How can we unambiguously conclude that is what the newline at hand means? If and only if the newline is followed by \w+=.

local $/ = ","; my $dn = ""; while(<>) { my $complete_dn; if(m/ \A (.+) \n ( \w+ = .+ ,) \z /sx) { $complete_dn = $dn . $1; $dn = $2; } else { $dn .= $_; } if($_ = $complete_dn) { s/\n+//g; do_whatever_with($_); } }

Update: This doesn't quite work. / \n \w+ = /x is not strong enough - what happens if it's the tag itself that's broken up? Then we get a dangling beginning of the tag as the end of the current DN and the rest of the tag with equals sign and value, newline and the second tag as the beginning of the new DN. However, since we read from comma to comma, we can garantuee that we always get the beginning of a tag at the start of the string. Therefor requiring that an equals sign precede the newline clarifies this corner case:

local $/ = ","; my $dn = ""; while(<>) { my $complete_dn; if(m/ \A (.+ = .+) \n ( \w+ = .+ ,) \z /sx) { $complete_dn = $dn . $1; $dn = $2; } else { $dn .= $_; } if($_ = $complete_dn) { s/\n+//g; print $_, "\n"; } }

Here's my "test suite":

#!/usr/bin/perl -w use strict; undef $/; my $data = <DATA>; my @munged = ($data) x length $data; substr $munged[$_], $_, 0, "\n" for 0 .. $#munged; print @munged; __END__ cn=Perl Monks,ou=Dining Hall,ou=Monastery,c=Universe
I piped this script's output into the other to test it.

Makeshifts last the longest.


In reply to Re: Match a string that can contain a carriage return in a random position. by Aristotle
in thread Match a string that can contain a carriage return in a random position. by robartes

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.