我目前正试图在字符串上运行两个不同的正则表达式模式,以比较字符串是使用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