英文:
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论