简体   繁体   中英

RegExp exclude subdomains from matching

I have a below RegExp

[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[az]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=][A-Z0-9]{3,10})

It matches domain along with its subdomain and parameters are also needed to be provided in an address, however, I want to be able to exclude some subdomains from matching. I've tried using negative Lookhead in order to achieve this, but without success.

^((?!(domain|orsubdomain)).)*$

Texts which should match the pattern:

nicesite.abc.com/abcs

nicesite.com/abcs

and shouldn't match:

niceDomain.com/abcs

hello.orsubdomain.com/abcs

Any help would be much appreciated.

Try this:

\b(?!.*\.?(domain|orsubdomain)\/?.*)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]

It works, at least with your examples!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM