Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

G'day BillDowns,

You have a number of issues here. I've included a fair amount of detail below but refer to perlre for the full story.

  • You're not actually showing a regex but just a fragment of one (I'll assume "/PV1\|1\|O\|(.*?\|){3}\|/"). I'm not trying to be pedantic but I can only respond to what you've written: for all I know, "PV1\|1\|O\|(.*?\|){3}\|" may be part of a larger regex. Also, I have no idea what modifiers, if any, you've used.
  • The "." in ".*?" matches any character including a pipe ("|") character which isn't what you want. (That's a slight oversimplication: it doesn't match a newline character unless you used the "s" modifier.) So, ".*?" would be better as "[^|]*" (zero or more characters that aren't pipe characters).
  • You don't anchor the regex so it could match anywhere in the string. To match at the beginning of the string you'll need to prepend "^" or "\A".
  • You've used capturing parentheses "( ... )" here. This won't break anything as it currently stands but could become an issue if you do want to capture fields later: "(?: ... )" (for clustering, not capturing) would be better.
  • Purely as a matter of style and personal taste, replacing the escaped pipe "\|" with the character class "[|]" may reduce what's been referred to as backslashitis and improve readability. Either is fine, it's up to you.

Putting all that together, you end up with a few options. Minimal changes would give: "/^PV1\|1\|O\|(?:[^|]*\|){3}\|/".

Having said all that, I'm wondering if splitting the lines on pipe characters might just be a whole lot easier in terms of general readability and future maintenance. Something along these lines:

my @fields = split /[|]/ => $line; ... if ($fields[0] eq 'MSH' and $fields[8] eq 'ADT^A02') { ... } ... if ($fields[0] eq 'PV1' and $fields[6] eq '') { ... } ...

-- Ken


In reply to Re: Selecting HL7 Transactions by kcott
in thread Selecting HL7 Transactions by BillDowns

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2014-08-28 08:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (259 votes), past polls