Reputation:
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
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\\.)?", "");
?
after that "s"Upvotes: 0
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
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