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
then you can change it by using a noncapturing set of parens (see perlre). For example:next unless $line =~ m{^(\S+) (\d+) (.*)};
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |