You may need nested grouping toward the content based on the end of the spec, but at least the early part of a METAR should give you no concern.

The following approach embodies the advice (brilliant minds come to similar solutions?) re breaking the METAR or SPECI down into tokens:

#!/usr/bin/perl -lw use strict; my $metar = 'METAR CCCC 210855Z AUTO dddff(f)Gfmfm(fm)KTv dndndnVdxdxd +x VVVVVSM [RDRDR/VRVRVRVRFT ] w\'w\' [NsNsNshshshs or VVhshshs T\'T\' +/T\'dT\'d APHPHPHPH'; if ( $metar =~ /^(METAR|SPECI)\s([A-Z]{4})\s(\d\d)(\d{4}Z)\s(AUTO|COR) +.*/) { my $type= $1; my $stn= $2; my $monthday = $3; my $obsTime= $4; my +$subtype= $5; print $type . " Station: $stn, Date: $monthday, ObservationTime: $ +obsTime, Subtype: $subtype"; } else { print "ww screwed up the regex."; }
($metar is taken straight from the ref doc, with digits substituted for YYGGgg and with the ticks escaped, as comprehensively understanding the spec would require more time than I have to give, just now.)

In reply to Re: Speeding up a large regex match pattern by ww
in thread Speeding up a large regex match pattern by wossname

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.