从一堆文本中分离https://*something*.com
本文关键字:https com 分离 something 文本 一堆 | 更新日期: 2023-09-27 18:03:37
我有一个:string BunchOfText
,它包含一个链接,以https://
开始,以.com
结束。我需要隔离那个链接并把它放在另一个字符串中。有什么建议吗?编辑:我的文本是这样的:
它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表格的发布而流行起来,最近有桌面出版软件,如Aldus PageMaker,包括Lorem Ipsum的版本。https://mydomain/RANDOMGENERATEDTEXT.com我们为什么要用它?
我想要一个新的字符串
string link ="https://mydomain/RANDOMGENERATEDTEXT.com"
在这个编辑的时候,user: serhiyb给了我一个完美的答案!
Regex linkParser = new Regex(@"https:'/'/(www'.)?[-a-zA-Z0-9@:%._'+~#=]{2,256}'.com'b([-a-zA-Z0-9@:%_'+.~#?&//=]*)?", 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()
并提取"in between"
类似:
string AllText = "fhdsfhhttps://what you want to extract.comDFDSFDSF";
var FirstIndex = AllText.IndexOf("https://");
var SecondIndex = AllText.IndexOf(".com");
您可以使用Regex
来定位链接,然后在链接中使用组来获取您想要的部分。
Regex: https:'/'/((www'.)?[-a-zA-Z0-9@:%._'+~#=]{2,256})'.com
括号中的部分是组。
在c#代码中,这样使用:
Regex regex=new Regex(@"https:'/'/((www'.)?[-a-zA-Z0-9@:%._'+~#=]{2,256})'.com");
foreach(Match match in regex.Matches("test for https://www.domain.com"))
string partBetween=match.Groups[1].Value; // www.domain