Search code examples
regexregexp-replace

How to replace part of a URL with regex


I need to remove part of a URL with a regex. From the words: http or https to the word .com. And it can be several times in one string. Can anyone help me with this?

For example a string: "The request is:https://stackoverflow.com/questions" After the removal - "The request is:/questions"


Solution

  • The regex that performed the deletion perfectly is: (@"\w+://[^/$]*") with replace "".

    Something like that:

    var regex = new Regex(@"\w+:\/\/[^\/$]*");
    regex.Replace(url, "");