簡體   English   中英

帶有 OR 運算符的 C# 多正則表達式模式

[英]C# Multiple Regex Patten with OR operator

我想找到我的傳入 URL 與這個或那個模式匹配。 你能幫我找到正確的模式嗎? 下面是我的代碼。 由於某種原因,它不起作用。

CurrentURL ="http://localhost/directoryA/directoryAA/Page/10";
string pattern1 = @"directoryA/directoryAA/(\w+)/(\d+)|directoryB/directoryBB/(\w+)/(\d+)";
Match matchfound = Regex.Match(CurrentURL.ToString(), pattern1, RegexOptions.IgnoreCase);

關於您期望什么輸入的信息並不多,但是通過稍微重寫模式以避免重復,它似乎可以按預期工作:

string currentURL = "http://localhost/directoryA/directoryAA/Page/10";
string pattern = @"/((directoryA/directoryAA)|(directoryB/directoryBB))/(\w+)/(\d+)";
Match match = Regex.Match(currentURL, pattern, RegexOptions.IgnoreCase);

if (match.Success)
{
    string directory = match.Groups[1].Value;
    string page = match.Groups[5].Value;
}

您可以使用在線工具來幫助測試正則表達式,例如https://regexr.com/http://regexstorm.net/tester

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM