我目前正試圖在字符串上運行兩個不同的正則表達式模式,以比較字符串是使用C#的myspace.com還是last.fm url。檢查多個正則表達式模式的字符串
我得到它的工作使用單一的正則表達式用下面的代碼:
public string check(string value)
{
Regex DescRegexShortened = new Regex(@"myspace\.com\/([a-zA-Z0-9_-]*)");
Match DescRegexShortenedMatch = DescRegexShortened.Match(value);
string url = "";
if (DescRegexShortenedMatch.Success)
{
url = DescRegexShortenedMatch.Value;
}
return url;
}
現在我的問題,是有simplier方法來檢查,如果字符串或者是myspace.com或last.fm網址?
Regex DescRegexShortened = new Regex(@"myspace\.com\/([a-zA-Z0-9_-]*)");
Regex mySpaceRegex = new Regex(@"last\.fm\/([a-zA-Z0-9_-]*)");
例如像:
如果字符串匹配regex1的或regex2然後...
看起來你只關心last.fm或myspace.com,而不是在那之後。你不只是做一個字符串.StartsWith或string.Contains而不是正則表達式模式?或者你的情況通常需要正則表達式嗎? –
您的正則表達式在'myspace.com?a = b'上失敗。邁克爾是正確的。 – linden2015
我目前正在嚮應用程序上傳一個包含用戶名的文本文檔。現在應用程序將使用API來接收Twitter用戶的生物和網站。現在,每個站點都將被添加到C#列表中,並且將通過這兩個正則表達式模式運行,這些模式檢查url是myspace還是last.fm url因此,它後面的用戶名並不重要 – stackquestiionx