简体   繁体   中英

Regex match a preceding exact text or nothing

i'm trying to execute a regex which will search in a domains list and check if the provided domain is a white list or not

let's say my main domain www.testDomain.com

I need a regex to match all subdomains and with different extentions, i've tried that and it works well, here's the regex:

"([a-z0-9]*.)([a-z0-9]*.)testDomain.(com|cn|local)"

But i have a case in which, i have the following domain

xxx.test-testDomain.com

i need to match the exact test- text or nothing,because i might have a domina xxx.anyString-testDomain.com , which is not allowed to pass , i've tried the following one

([a-z0-9]*.)((?!.test-|(?!)))testDomain.(com|cn|local)

but doesn't work, any suggestion to do it?

Cases:

http://abc.impersonate-testdomain.com -- shouldn't pass
http://testdomain.com -- ok
http://abc.testdomain.com -- ok
https://abc.testdomain.com -- ok
https://abc.test-testdomain.com -- ok
http://abc.testdomain.local:8081 -- ok

Try below regex:

([a-z0-9]*\\.)?(test-)?testdomain.(com|cn|local)

在此处输入图片说明

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