How can I match words that are not specific words?

Using a Perl or unix regex, how would I capture a word that is not a range of values. This is what I am trying to achieve.

(\w:not('int','long'))

      

+2


source to share


2 answers


Not sure if this is valid perl syntax, but in a "general" taste you might say

/\b(?!int\b|long\b)\w+\b/

      



If you want to spell a word, put parens around \w+

like this

/\b(?!int\b|long\b)(\w+)\b/

      

+9


source


It is generally faster to say:

my %exclude = map { $_ => 1 } qw/int long/;
my @words   = grep { not exists $exclude{$_} } /(?:\b|^) (\w+) (?:\b|$)/gx;

      



especially in versions of Perl prior to 5.10 (when interleaving received a significant speed increase).

+6


source







All Articles