in reply to Modifying a regex
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modifying a regex
by seni (Initiate) on Oct 27, 2006 at 18:03 UTC | |
|
Re^2: Modifying a regex
by seni (Initiate) on Oct 27, 2006 at 18:31 UTC | |
by grep (Monsignor) on Oct 27, 2006 at 19:34 UTC | |
by Anonymous Monk on Oct 27, 2006 at 20:15 UTC | |
by grep (Monsignor) on Oct 27, 2006 at 21:00 UTC | |
by seni (Initiate) on Oct 27, 2006 at 20:21 UTC |