Your first problem is that you are looking for two single letters but using \d in your pattern each time, which means a digit; use a character class of [A-Z] (or [A-Za-z] if you expect lower case as well). Secondly, you are using parentheses to capture your three floating point numbers but you dont seem to be using the captures afterwards. Thirdly, a dot is a regular expression metacharacter matching any character (with caveats, see perlretut and perlre) so you need to escape it to match a literal dot. Fourth, using \t is fine if you are absolutely certain that you will only ever have a single tab as a separator; \s+ for one or more white spaces is more robust. Fifth, /SEQ+/ means an 'S', an 'E' then one or more 'Q's and if the 'SEQ' should be at the beginning of the line the pattern should be anchored with a caret; so /^SEQ.+/ might be better.

You are likely to have far more data lines than header lines so it wil save cpu ticks to put that condition first.

... if( $csv_line =~ /([A-Z])\s+([A-Z])(?:\s+\d\.\d{3}){3}/ ) { ... } elsif( $csv_line =~ /^SEQ.+/ ) { ... } else { # What do you do with a line that matches neither pattern? } ...

I hope these points are useful.

Cheers,

JohnGG


In reply to Re: Regular Expression Problem by johngg
in thread Regular Expression Problem 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.