正则表达式删除附加在实际URL上的URL

huangapple go评论64阅读模式
英文:

Regex to remove an appended URL from an actual URL

问题

I have created a regular expression pattern to remove the appended URL from the given string. Here's the modified string:

给定字符串 https://test1-sc10-auth.comhttps://test1-sc10.com/search-results#e=0&q=news,我要移除附加的URL https://test1-sc10.com,使结果为 https://test1-sc10-auth.com/search-results#e=0&q=news

我已经创建了一个正则表达式模式,用于实现这个目标。以下是修改后的字符串:

https://test1-sc10-auth.com/search-results#e=0&q=news

希望对你有帮助。

英文:

I'm looking for a regular expression pattern that can remove a URL that is appended to the end of an actual URL.

For example, given the string https://test1-sc10-auth.comhttps://test1-sc10.com/search-results#e=0&q=news, I want to remove the appended URL https://test1-sc10.com so that the result is https://test1-sc10-auth.com/search-results#e=0&q=news.

I have tried using various regex patterns, but the closest result I got was https://test1-sc10.com/search-results#e=0&q=news using the pattern ^(.*?)((?:https?://\S+))(?:.*?\2)+?(.*)$.

Any suggestion would be appreciated.

Thanks in advance.

答案1

得分: 0

You should find what is happening, and also regex is an overkill to do this as we explain in the comments, try with this code:

Fiddle: https://dotnetfiddle.net/l7szjf

string badurl = "https://test1-sc10-auth.comhttps://test1-sc10.com/search-results#e=0&q=news"; //your original url
int position = badurl.IndexOf("http", 6); //get the position of any http or https after the first one, assuming you only have only 2 times repeated the code.
string goodurl = badurl.Substring(position); //get everything after that position
Console.WriteLine(goodurl); //test it

You can solve it in one line:

url = url.Substring(url.IndexOf("http", 6));

英文:

You should find what is happening, and also regex is an overkill to do this as we explain in the comments, try with this code:

Fiddle: https://dotnetfiddle.net/l7szjf

	string badurl = "https://test1-sc10-auth.comhttps://test1-sc10.com/search-results#e=0&q=news"; //your original url
	int position = badurl.IndexOf("http",6); //get the position of any http or https after the first one, assuming you only have only 2 times repeated the code.
	string goodurl = badurl.Substring(position); //get everything after that position
	Console.WriteLine(goodurl); //test it

You can solve it in one line:

url = url.Substring(url.IndexOf("http",6));

huangapple
  • 本文由 发表于 2023年4月20日 07:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定