2016-02-29 66 views
0

我必須阻止所有的URL *.company.com,但保留proxy.company.com可訪問。我怎麼能用正則表達式來做到這一點?正則表達式:只排除一個範圍的一個URL

[^proxy].company.com不起作用,我不知道爲什麼。

謝謝。

+0

是它谷歌分析或的.htaccess? –

+0

用什麼語言? – Braj

+0

所有當前答案都使用lookahead,並且在GA中不起作用。 –

回答

0

可以使用Negative Lookahead

demo

Java示例嘗試^(?!proxy)[^.]+\.company\.com$

String regex = "^(?!proxy)[^.]+\\.company\\.com$"; 

    System.out.println("abc.company.com".matches(regex)); // true 
    System.out.println("xyz.company.com".matches(regex)); // true 
    System.out.println("proxy.company.com".matches(regex)); // false 

正則表達式說明:

^      the beginning of the string 
    (?!      look ahead to see if there is not: 
    proxy     'proxy' 
)      end of look-ahead 
    [^.]+     any character except: '.' (1 or more times 
          (matching the most amount possible)) 

    \.      '.' 
    company     'company' 
    \.      '.' 
    com      'com' 
    $      before an optional \n, and the end of the string