如何使用c#

时间:2019-04-26 12:06:11

标签: c# regex

1 个答案:

答案 0 :(得分:0)

尝试使用使用正则表达式的方法:

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();
}