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 關注
- 84 瀏覽
添加回答
舉報