开发者

How to use if-then in regular expressions?

I'm writi开发者_开发百科ng a regular expression for Postfix that defines its virtual domains map. I want to catch all subdomains of a domain except for two.

Assuming my domain is example.com and the two exclusion subdomains are in,mail, I wrote the following regular expression:

(?(?!mail|in).+\.example.com)

It supposed to recognize whatever.example.com but not in.example.com or mail.example.com.

It does work in RegExr, but it doesn't work in Postfix nor Ruby. I'm assuming that I'm using if-then wrong, what is the correct syntax? Are there other options?


If you use an anchor, then it should work.

\b(?!mail|in).+\.example.com

See it here on Rubular

The anchor at the beginning \b is a word boundary. This assures that there is non word character before your subdomain and therefor it does not match ail from mail anymore.

This matches

www.example.com
whatever.example.com

and not

mail.example.com
in.example.com

UPDATE

Probably the ^ anchor is the better choice here. This would match the start of the string.

^(?!mail|in).+\.example.com

For your examples it makes no difference, but if your URL starts with a non-word character then it would be wrong with the word boundary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜