简体   繁体   中英

How do I remove only the url from text and Ignore other url in c#

I would like to remove only the last instance of the URL in a string in c#.

Example String: "sample text http://www.url1.com sample text https://www.url2.com sample text http://www.url3.com"

I would like to remove only "http://url3.com" , and keep the other URL in the string.

Will some combination of string functions and regex will help achieve the same? I tried regex, but it removes all instances of the URL.

Edit : This involves matching the last URL (which is random each time ) and removing i.

@GaurangDave answer worked well

I used generic Regex pattern to find url from text. You can change according to your need. This example works for your scenario. It will remove last url from string.

string txt = "sample text http://www.url1.com sample" +
             "text https://www.url2.com sample text " +
             "http://www.url3.com";

var matches = Regex.Matches(txt, @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");

txt = txt.Replace(matches[matches.Count - 1].Value, string.Empty);

You can match the last URL using this regex,

http\S*$

And replace it with empty string.

Demo1

If optionally there can be space after last URL, you can optionally match it using this regex,

http\S*\s*$

Demo2

In case you want to support more protocols, you can have alternation in regex specifying different protocols like this,

(?:file|ftp|http)\S*\s*$

Demo3

C# Sample codes,

string str = @"sample text http://www.url1.com sample text https://www.url2.com sample text http://www.url3.com";
string replacedStr = Regex.Replace(str, @"(?:file|ftp|http)\S*\s*$", "");
Console.WriteLine("Result: " + replacedStr);

Prints,

Result: sample text http://www.url1.com sample text https://www.url2.com sample text

Here's a non-regex solution that also works if you have extra text after the last URL:

string input = "sample text http://www.url1.com " +
               "sample text https://www.url2.com " +
               "sample text http://www.url3.com " +
               "extra text";
int pos = input.LastIndexOf("http://", StringComparison.InvariantCultureIgnoreCase);
string lastURL = 
    new string(input.Substring(pos).TakeWhile(c => !char.IsWhiteSpace(c)).ToArray());
string output = input.Substring(0, pos) + input.Substring(pos + lastURL.Length);

Console.WriteLine("Last URL: " + lastURL);
Console.WriteLine("Cleaned text: " + output);

Output:

Last URL: http://www.url3.com
Cleaned text: sample text http://www.url1.com sample text https://www.url2.com sample text  extra text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM