从一堆文本中分离https://*something*.com
问题描述:
我有一个:string BunchOfText
其中包含一个以https://
开头并以.com
结尾的链接。 我需要隔离该链接并将其放入另一个字符串中。 有什么建议如何? 编辑: 我的文字是这样的:从一堆文本中分离https://*something*.com
它与桌面排版软件,如奥尔德斯PageMaker中包括Lorem存有版本的普及在1960年代与含Lorem存有通道Letraset张的释放,以及最近。 https://mydomain/RANDOMGENERATEDTEXT.com 我们为什么要使用它?
我想有一个新的字符串
string link ="https://mydomain/RANDOMGENERATEDTEXT.com"
此编辑的时候,用户:serhiyb,给了我一个完美的答案!
答
Regex linkParser = new Regex(@"https:\/\/(www\.)?[[email protected]:%._\+~#=]{2,256}\.com\b([[email protected]:%_\+.~#?&//=]*)?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string rawString = "some text with https://go.com link in it";
foreach(Match m in linkParser.Matches(rawString))
Console.WriteLine(m.Value);
现场演示: https://dotnetfiddle.net/Zg8UDj
它会发现,与https
开始,是.com
区的子域中的所有环节。
答
你可以裁剪字符串如下:
string text = "https://what you want to extract.com";
string extr = text.Substring(8, text.Length-12);
extr
是你想要的,因为我认为字符串。
答
您需要两次使用IndexOf()
并提取“之间”。
喜欢的东西:
string AllText = "fhdsfhhttps://what you want to extract.comDFDSFDSF";
var FirstIndex = AllText.IndexOf("https://");
var SecondIndex = AllText.IndexOf(".com");
答
您可以使用Regex
找到链接,然后一组链接里面得到的只是你想要的部分。
正则表达式:https:\/\/((www\.)?[[email protected]:%._\+~#=]{2,256})\.com
在括号中的部分是基团。
在C#代码,这是使用这样的:
Regex regex=new Regex(@"https:\/\/((www\.)?[[email protected]:%._\+~#=]{2,256})\.com");
foreach(Match match in regex.Matches("test for https://www.domain.com"))
string partBetween=match.Groups[1].Value; // www.domain
做*所有*的字符串开始'的https://'和'与结束.com'?在这种情况下,你可以做'myUrl.Substring(8,myUrl.Length - 12)' –
你能告诉我们字符串和你试过了什么吗? – Muckeypuck
@Bob Kaufman我有一堆文本,并在文本中间我有一个链接,其长度不是恒定的。因此,我必须通过“开始”和“结束”来隔离该部分。 – qretsar