繁体   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