Reputation: 3189
How do I make regex match and exclude certain character. For example, I am making an regex for all country's TDL match.
[a-z]{1,}((?!.ru)|.kr|.lt|.cn|.uk|.id|)
or
[a-z]{1,}.([a-z]{1,})
gsdfg.ru <- This shall match
sgkjsr.us <- only .us should not match or should excluded
Upvotes: 0
Views: 3512
Reputation: 11601
This should work (blacklisting):
\.(?!us|eu)[a-z]{2}$
It searches for a dot (\.
), which isn't followed by us
or eu
((?!us|eu)
), and is followed by two lowercase letters ([a-z]{2}
), right at the end of the string ($
).
This should also work (whitelisting):
\.(?:ru|kr|cn|uk|id)$
It searches for a dot (\.
), which is followed by one of ru
, kr
... or id
, right at the end of the string ($
). I used (?:...)
instead of a simple (...)
so that the group doesn't create a capture. If you actually need to capture the country code for later user, feel free to remove the ?:
part.
Upvotes: 3