How to test string with regular expression in C#?
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))
精彩评论