1 回答
TA贡献1829条经验 获得超7个赞
试试这个使用正则表达式的方法:
static void Main(string[] args)
{
var urls = new string[] { "https://www.example.com/*/post/", "https://www.example2.com/videos/*" };
// here regex patterns are created: special characters are escaped and
// star, which means here "anything" is replaced by .+ which means "one or more of any charaters"
var regexes = urls.Select(url => new Regex(url.Replace("/", @"\/").Replace(".", @"\.").Replace("*", ".+")));
var toCheck = new string[]
{
"https://www.example.com/user123/post/",
"http://www.example.com/user123/post/abc",
"https://www.example2.com/videos/1234",
"https://www.example2.com/videos/1234/5678",
"http://www.example2.com/videos/1234/5678",
"http://example2.com/videos/1234/video.asp?id=1"
};
var valid = toCheck.Where(url => regexes.Where(r => r.Match(url).Success).Any()).ToArray();
}
- 1 回答
- 0 关注
- 66 浏览
添加回答
举报