in reply to Grep 1st word of strings in array
In addition to the other comments, there are some problems with your regex. This will not match, for instance, because the # character isn't matched by either \w or \s:
#!/usr/bin/env perl use Modern::Perl; my $line = '>foo# bar'; if( $line =~ /^>(\w+)\s(.+)$/ ){ say "Found word $1"; } else { say "No match."; }
So it depends on what you mean by a "word." If you mean just characters matched by \w, then the opposite of that is \W. If you mean "non-whitespace," you should be using \S to match that. Also, that match is greedy, so there's no need to follow it with an opposite match in the first place. These will work just fine, and be much more legible:
$line =~ /^>(\w+)/; # capture as many consecutive \w characters as p +ossible following > $line =~ /^>(\S+)/; # capture as many consecutive non-whitespace char +s as possible following >
Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Grep 1st word of strings in array
by Marshall (Canon) on Mar 08, 2012 at 00:03 UTC |