简体   繁体   中英

Regex: Match everything except string

I've got the regex

^(.)+?domain\.com

which machtes

www.domain.com or
domain.com or
something.domain.com but
.domain.com too.

What I want is to "filter out" the possibility of ".domain.com". It should match [something].domain.com and domain.com but NOT .domain.com. (or ..domain.com). It should be a valid subdomain. What would be the correct regex to get this done?

Updated question to clarify requirements.

这种模式可以做到

^([^.]+\.)*domain\.com

Actually it will match otherdomain.com too. A better approach might be to allow zero or one occurrences of non-dot+ dot:

^([^.]+\.)?domain.com$

As suggested in a comment, to allow many levels of subdomains, change ? to * .

这将允许多个子域和域本身

^(.*\.)?domain.com&

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