亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能將部分 url 與 c#

我怎樣才能將部分 url 與 c#

C#
桃花長相依 2022-12-31 11:21:30
我有一個動態列表,如下幾行https://www.example.com/*/post/https://www.example2.com/videos/*https://www.example3.com/photo/我如何匹配 C# 以下 url(返回 true)https://www.example.com/user123/post/http://www.example.com/user123/post/abchttps://www.example2.com/videos/1234https://www.example2.com/videos/1234/5678http://www.example2.com/videos/1234/5678http://example2.com/videos/1234/video.asp?id=1也許我可以使用 UriTemplate 但 url 有很多部分。他們沒有靜態格式。
查看完整描述

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

}


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 84 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號