Greetings, oh high ones...

I've been working on this for a while today, and I must just be missing something obvious

I'm receiving a string of characters that contains occasional 'special characters' (two slashes with an alpha character between them) that I need to convert back to their regular form.

Specifically, I need to convert:

\F\ to | \S\ to ^ \T\ to & \R\ to ~ \E\ to \
If you've ever worked with HL7 messaging protocol, you may have seen this before. (Basically, these five characters are message control characters, so the \x\ notation is how it lets you know to replace them with the reserved character)

Anyways, I started with the following:

$msgu =~ s/\\F\\/\|/g; $msgu =~ s/\\S\\/\^/g; $msgu =~ s/\\T\\/\&/g; $msgu =~ s/\\R\\/\~/g; $msgu =~ s/\\E\\/\\/g;
And that *almost* works perfectly. Where it doesn't work is if there's two of these special characters grouped around a normal character... For example, the string:
"AB\T\F\S\CD"
should translate to:
"AB&F^CD"
but it ends up as:
"AB\T|S\CD"
because I'm doing the global match on \F\ first. And since I can get these in any order, there's no way to put them in an order that always works.

So, what I *really* need is a way to do this 'left-to-right' where it finds the first \x\ character, and then translates it (if it's one of the valid 5), and then continue from there. Doing whichever comes up next in the string

Any advice for me to try or place I should be looking?

Trek


In reply to Pattern Matching, left-to-right by TrekNoid

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.