If you simply want to strip the NA from the beginning of a string, you can use s/PATTERN/REPLACEMENT/ (see perlop). This will not affect IDs that do not start with NA. For example,

use strict; use warnings; for ( 'NA12345', 67890 ) { my $id = $_; print "$id -> "; $id =~ s/^NA//; print "$id\n"; } __END__ NA12345 -> 12345 67890 -> 67890

Update: I think I misread the question. If you want to allow an optional NA in the line that reads

next unless $line =~ m{^(\S+) (\d+) (.*)};
then you can change it by using a noncapturing set of parens (see perlre). For example:
use strict; use warnings; for( 'string1 NA12345 other stuff', 'string2 67890 more stuff' ) { if( $_ =~ m/^(\S+) ((?:NA)?\d+) (.*)/ ) { print "matched: $2\n"; } } __END__ matched: NA12345 matched: 67890

Update 2: It looks like your regex is simply capturing 3 fields separated by a single space. If that is the case, split might be more appropriate.

use strict; use warnings; for( 'string1 NA12345 other stuff', 'string2 67890 more stuff' ) { my @elements = split( /\s/, $_, 3 ); print( '[', join( '][', @elements ), "]\n" ); } __END__ [string1][NA12345][other stuff] [string2][67890][more stuff]

HTH


In reply to Re: Modifying a regex by bobf
in thread Modifying a regex by seni

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.