2

So far I have

messageText1 = Regex.Replace(messageText1, "(www|http|https)*?(com|.co.uk|.org)", "[URL OMITTED]");

With only the www, and without the bracks or http or https it works as intended

For example and input of Hey check out this site, www.google.com, it's really cool would output hey check out this site, [URL OMITTED], it's really cool

But if I put back in the or operators for the start of the URL, it only replaces the .com part of the input

Why won't it work?

Thanks

Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43

4 Answers4

3
(www|http|https)*?(com|.co.uk|.org)

means www or http or https 0 to many times immediately followed by com .co.uk or .org. So it would match for example httphttphttp.co.uk

Your intention was probably just to have a . before the *. Which then means it only looks for (www|http|https) once, then it matchs . (any character) 0 to many times.

You are also missing the . in .com. However, if you want to match a literal . you need to use \., since a . on its own means 'any character'.

With that in mind, the regex I think you were going for is:

(www|http|https).*?(\.com|\.co\.uk|\.org)
OGHaza
  • 4,795
  • 7
  • 23
  • 29
1

Your expression is missing a . somewhere or (possibly better) a \S+

 (www|http|https)\S*(com|\.co\.uk|\.org)

In C#:

 Regex.Replace(messageText1, @"(www|http|https)\S*(com|\.co\.uk|\.org)", "[URL OMITTED]");

Note: you probably want to escape the .'s as well.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
1

This should work better. It will also work for other TLDs that don't end with .com, .co.uk or .org:

messageText1 = Regex.Replace(messageText1, @"\b(?:http://|https://|www\.)\S+", "[URL OMITTED]");
geedubb
  • 4,048
  • 4
  • 27
  • 38
0

A simple version which i tried is as follows.

messageText1 = Regex.Replace(messageText1, @"(www)?(.)?[a-z]*.(com)", "[URL OMITTED]");

i tried this with

string messageText1 = " Hey check this out, http:\www.google.com,its cool";

string messageText1 = " Hey check this out, www.google.com,its cool";

string messageText1 = " Hey check this out, google.com,its cool";

Puneet
  • 753
  • 3
  • 9
  • 24