user9624111
user9624111

Reputation:

Pattern not correct with regular expressions in c#

i am doing a project , and i want to remove from a string http protocoll. In my excel sheet there are two types one is http://[email protected] and the other is http://[email protected] have tried so many combinations but i can't find the right one. My code only works with the first type and not with the second one

var website_domain_in_excel = list_of_information_in_excel[2];  
string pattern = "(http://\\www.)";
Console.WriteLine(Regex.Replace(website_domain_in_excel, pattern, String.Empty));

Thank you for your time

Upvotes: 0

Views: 49

Answers (3)

Hans Kesting
Hans Kesting

Reputation: 39255

A non-regex solution:

var eml = "http://[email protected]";
eml = eml.Replace("http://", "").Replace("www.", "");
// eml now is "[email protected]"

You might want to test that that "www." only appears at the start. The (unusual) "[email protected]" should remain intact.


But if you really want a regex:

eml = Regex.Replace(eml, "^https?://(www\\.)?", "");
  • This also catches "https", because of the ? after that "s"
  • It will also find and replace an optional "www.", but only at the start

Upvotes: 0

Dr.Jones
Dr.Jones

Reputation: 1

You can use the string: "http?://?www.|http?://" which matches either "http://www." or "http://".

The code would look like this:

var website_domain_in_excel = list_of_information_in_excel[2];  
string pattern = @"http:\/\/www.|http:\/\/";
Console.WriteLine(Regex.Replace(website_domain_in_excel, pattern, String.Empty));

Upvotes: 0

Charles Mager
Charles Mager

Reputation: 26213

The pattern you want is this:

string pattern = @"http:\/\/(?:www\.)?"

This matches http:// and then an optional non-capturing group matching www..

You can see an explanation of the regex here and this fiddle for a working demo in C#.

Upvotes: 3

Related Questions