开发者

How to test string with regular expression in C#?

开发者 https://www.devze.com 2023-03-06 08:21 出处:网络
I have URL link like \"images.myweb.com/files\" OR \"test.com/images\" I want to check if URL contains like

I have URL link like "images.myweb.com/files" OR "test.com/images"

I want to check if URL contains like

".com/" or ".in/" or ".net/" or ".org/" with regular expression .

H开发者_Python百科ow it possible with C# regular expression..


don't use a Regex, simply use an URI class wih a String.EndsWith()

Uri baseUri = new Uri("http://www.contoso.com:8080/images");
  bool isCom = baseUri.Host.EndsWith(".com");
  bool isNet= baseUri.Host.EndsWith(".net");
  bool isOrg = baseUri.Host.EndsWith(".org");

perhaps combined with a String.ToLower() to bypass case sensitivity.


lst is your string lists (like lst = new []{".com/" , ".in/" ,".net/" ,".org/"}) then you can do it with Linq:

lst.Any(x=>myString.Contains(x))

and if you want your string end with this items do:

lst.Any(x=>myString.EndsWith(x))
0

精彩评论

暂无评论...
验证码 换一张
取 消