简体   繁体   中英

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 .

How 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM