2011-11-01 229 views
0

我陷入了一個簡單的正則表達式問題。 我想要做的是檢查一個URL,看它是否包含單詞ajax,如果它匹配了URL。正則表達式網址檢查

例如 給定的URL本地主機/ AJAX /圖像/獲取 應該匹配,並返回本地主機/ AJAX /圖像/獲取

不過本地主機/圖像/獲取 應該不匹配。

我有正則表達式的基本知識,但沒有能夠得到任何工作。

有什麼建議嗎?我相信這是一個相當簡單的例子。

我用這對國防部重寫規則

國防部重寫規則重寫規則 ^。 ?\ bajax \ b。 $ ajax.php?URL = $ 1 [PT,L]

URL請求http://localhost:93/public/ajax/image/

它重定向到ajax.php文件,但URL值不會保留。

我有另一個規則是如下 重寫規則^(。*)$的index.php?URL = $ 1 [PT,L]

URL請求http://localhost:93/public/imgage/get/

當該重定向到一個普通index.php文件有/公/圖像/獲取/ url中的GET價值

+0

將'/ ajax /'永遠作爲這樣的請求的根文件夾? – Clive

+0

不總是,只要它的部分url – madlad

回答

1

我解決了這個問題,我正在尋找的正則表達式是 ^(。ajax。()$

我一直在嘗試^(ajax)$但這沒有任何意義。

感謝您的幫助

0

什麼

[a-z/]*ajax[a-z/]* 

它可以使用一些細化,所以讓我硝酸鉀如果需要改進的話。

+0

我不認爲這將匹配,因爲我不認爲它不會匹配http://部分。我已經試過這個,它不符合 – madlad

+0

是xxxx://部分總是會在那裏?如果是這樣,請嘗試[az] +:// [az ._/- ] * ajax [az ._/- ] * –

1

簡單的正則表達式。你有多個標籤,所以我不太清楚你的平臺是什麼。如果是用java:

Pattern regex = Pattern.compile("^.*?\\bajax\\b.*$"); 

一般來說,這應該是正則表達式:

/^.*?\bajax\b.*$/ 

說明:

"^" +  // Assert position at the beginning of the string 
"." +  // Match any single character that is not a line break character 
    "*?" +  // Between zero and unlimited times, as few times as possible, expanding as needed (lazy) 
"\\b" +  // Assert position at a word boundary 
"ajax" + // Match the characters 「ajax」 literally 
"\\b" +  // Assert position at a word boundary 
"." +  // Match any single character that is not a line break character 
    "*" +  // Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
"$"   // Assert position at the end of the string (or before the line break at the end of the string, if any) 

最後我建議this進一步閱讀。

+0

嗨,感謝您的幫助,這是我正在嘗試的正則表達式之一, 。然而,使用netbeans插件來測試正則表達式和Apache mod_rewrite URL,我將放置規則的位置似乎都不匹配。我有4個標籤,因爲我必須有4個標籤才能提出問題。我會發布我的mod_rewrite規則和URL我打 – madlad

0

它應該是這麼簡單:

Pattern.matches(regex, str) 

所以

Pattern.matches( 「AJAX」,STR)

其中str被傳入的URL字符串

+0

您好保羅,我用它在一個國防部重寫,因爲我在一個netbeans插件測試正則表達式,我需要把4個標籤放在我把問題java作爲標籤。請參閱上面的額外註釋 – madlad

+0

@Paul這也將匹配blajaxbla – FailedDev

+0

@failedDev如果您使用「ajax」,則不會。 – Standage