简体   繁体   中英

Regular expression that match everything but two words

I have this regular expression:

^(.*)$ https://mysite.com$1

It matches everything and then convert to:

anything.mysite.com/foo  --- >   mysite.com/foo

I will like to do something similar but only if the anything word is different from 2 specific words, I will like to execute that rewrite only if the sub-domain is different from string1 or string2. So:

string1.mysite.com/query     --->   https://string1.mysite.com/query
foo.mysite.com/query         --->   https://mysite.com/query

Since you are using Nginx here is what you can add in server block to disallow matching these 2 subdomains:

server_name ~^(?!string1|string2)[^.]+\.mysite\.com$;

(?!string1|string2) is negative lookahead that will match anything but string1 or string2 .

You might be able to do it with lookbehinds.
To remove the part you don't want, just replace with $1$2

 # ^(.*?(?://))?.*?(?<!\bstring1)(?<!\bstring2)\.(mysite\.com\b.*?)$

 ^ 
 (                             # (1 start)
      .*? 
      (?: // )
 )?                            # (1 end)
 .*? 
 (?<! \b string1 )
 (?<! \b string2 )
 \.
 (                             # (2 start)
      mysite\.com \b 
      .*? 
 )                             # (2 end)
 $

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