-1

I have a :string BunchOfText which contains a link which starts with https:// and ends with .com. I need to isolate that link and to put it in another string. Any suggestions how? EDIT : My text looks like this:

It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. https://mydomain/RANDOMGENERATEDTEXT.com Why do we use it?

I want to have a new string

string link ="https://mydomain/RANDOMGENERATEDTEXT.com"

By the time of this edit, user : serhiyb, gave me a perfect answer!

qretsar
  • 13
  • 5
  • Do *all* the strings start with `https://` and end with `.com`? In that case you could do `myUrl.Substring( 8, myUrl.Length - 12 )` – Bob Kaufman Aug 24 '16 at 01:00
  • Could you show us the string and what you've tried? – Muckeypuck Aug 24 '16 at 01:02
  • @Bob Kaufman I have a bunch of text and in the middle of the text I have a link whose length is not constant. Therefore I have to isolate that part by "start with" and "end with". – qretsar Aug 24 '16 at 01:04
  • Please edit your question to include/clarify this important information. – Bob Kaufman Aug 24 '16 at 01:07

4 Answers4

0

you can just crop the string as following:

string text = "https://what you want to extract.com";
string extr = text.Substring( 8, text.Length-12 );

extr is the string you want as I think.

Gry
  • 54
  • 3
0
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);

Live demo: https://dotnetfiddle.net/Zg8UDj

It will find all links that start with https and are subdomain of .com zone.

serhiyb
  • 4,753
  • 2
  • 15
  • 24
0

You will need to use IndexOf() twice and extract the "in between".

Something like:

string AllText = "fhdsfhhttps://what you want to extract.comDFDSFDSF";

var FirstIndex = AllText.IndexOf("https://");
var SecondIndex = AllText.IndexOf(".com");
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
0

You can use Regex to locate the links, and then a group inside the link to get just the part that you want.

Regex: https:\/\/((www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256})\.com

The part in the parenthesis is the group.

In C# code, this is used like this:

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
GregorMohorko
  • 2,739
  • 2
  • 22
  • 33