Abhishek B.
Abhishek B.

Reputation: 5202

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..

Upvotes: 1

Views: 210

Answers (2)

Caspar Kleijne
Caspar Kleijne

Reputation: 21864

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.

Upvotes: 5

Saeed Amiri
Saeed Amiri

Reputation: 22565

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

Upvotes: 2

Related Questions