How to use regex to search for something NOT a specific pattern

With Perl-style regexp, is it possible to search for something of an undefined pattern?

For example, [^abc] searches for a SINGLE CHARACTER, not a , not b and c .

But can I indicate something longer than one character?
For example, in the next line, I want to find the first word, which is not a top-level domain name, and does not contain an uppercase letter or, perhaps, more complex rules, such as the presence of 3-10 characters. In my example, this should be "abcd" :

 net com org edu ABCE abcdefghijklmnoparacbasd abcd 
+4
source share
3 answers

You can do this with negative pending expectations:

 ^(?!(?:net|com|org|edu)$)(?!.*[AZ])[az]{3,10}$ 

Take a look

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. 

Thus, the (?!(?:net|com|org|edu)$) ensures that the login is not one of the top-level domains.

The (?!.*[AZ]) ensures that the input does not have an uppercase letter.

The [az]{3,10}$ ensures that the input has a length of at least 3 and at most 10.

+5
source

Just use the do not match operator :! ~

So, just create your expression and then see that the variable does not match it:

 if ($var !~ /abc/) { ... } 
+4
source

IMHO it's easier to do with regular expression 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 (az might not be what you want though) foreach( $s =~ /(\b[az]{3,10}\b)/g ){ print $_, "\n" if is_tpl($_); } 

By the way, there are many top-level domains.

0
source

All Articles