How to Use Regular Expression to Look for the something NOT of Certain pattern
Using Perl style regexp, is it possible to look for something not of certain pattern?
For example, [^abc]
looks for a SINGLE CHARACTER not a
nor开发者_开发知识库 b
nor c
.
But can I specify something longer than a single character?
For example, in the following string, I want to search for the first word which is not a top level domain name and contains no uppercase letter, or perhaps some more complicated rules like having 3-10 characters. In my example, this should be"abcd"
:
net com org edu ABCE abcdefghijklmnoparacbasd abcd
You can do it using negative look-ahead assertions as:
^(?!(?:net|com|org|edu)$)(?!.*[A-Z])[a-z]{3,10}$
See it
Explanation:
^ - Start anchor
$ - End anchor
(?:net|com|org|edu) - Alternation, matches net or com or org or edu
(?!regex) - Negative lookahead.
Matches only if the string does not match the regex.
So the part (?!(?:net|com|org|edu)$)
ensures that input is not one of the top level domains.
The part (?!.*[A-Z])
ensures that the input does not have a upper case letter.
The part [a-z]{3,10}$
ensures that the input is of length atleast 3 and atmost 10.
Just use the "not match" operator: !~
So just create your expression and then see that a variable does not match it:
if ($var !~ /abc/) {
...
}
IMHO its easier to do some matching with regexp and some checks with perl.
#!/usr/bin/env perl
use strict;
use warnings;
my $s = "net com org edu ABCE abcdefghijklmnoparacbasd abcd";
# loop short words (a-z might not be what you want though)
foreach( $s =~ /(\b[a-z]{3,10}\b)/g ){
print $_, "\n" if is_tpl($_);
}
BTW, there are a lot of top level domains ..
精彩评论