" You use both "platform specific line-ending" and "network line-ending." Is the use of two different phrases accidental or purposeful?"

Very purposeful.

The sub will receive strings for transmission across the network. The caller may have terminated the string with "\n" (which will be the platform-sepecific line-ending dependant upon what the current platform is), or they may not have done so.

Before transmission, I need to:

  1. Add a network-specific line-ending (\015\012) if no line-ending is present.
  2. Convert whatever (platform-specific) line-ending is present, to the network line-ending is a line-ending, if one is present.
  3. Leave the string alone, if the correct line-ending is already present.

It seems like a single regex that used look-behind assertions properly would be able to do this, thereby avoiding messy conditional logic. It also seems like this is an oft-called for requirement and a well tried solution is probably already known.

I've had a couple of attempts at constructing the regex, but in each case, it falls over in one of the above three cases. Either adding an extra, unnecessary line-ending or omitting to add one.

I hoped someone in the know would point me at the correct regex to use?


In reply to Re^2: regex to add network line-endings if required. by Anonymous Monk
in thread regex to add network line-endings if required. by Anonymous Monk

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.