Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select substring after specific word

From strings like this

<iframe width="560" height="315" src="https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe>

I need to select only the source, so the word between src="the string I need"

I have tried using IndexOf the word src=" but the link doesn't have a fixed number of characters to set the ending.

like image 245
noga Avatar asked Apr 16 '15 07:04

noga


People also ask

How to get the result of the string after the substring?

So, the Substring after function reads the URL declared under the name=S and prints the result of the string after the substring. The following code shows how to use after function () to represent the delimiter with the numbers. Following is the next example showing Parenthesis values. The following code will result in the below output.

How to extract the substring after the reference character in PowerShell?

Finally, use the following PowerShell substring Method to extract the substring after the reference character $subdomain.Substring($refcharacter+1) As expected, the result extracts the substring after the period, which is itechguides.com !

How to extract text after a character in Microsoft Word?

2. RIGHT, LEN, and FIND Functions to Extract Text After a Character 3. Use of LEFT, FIND, and SUBSTITUTE Functions to Excerpt Text After a Character 4. Using RIGHT, SEARCH, and SUBSTITUTE Functions to Extract Specific Characters 5. Using RIGHT, SUBSTITUTE, and REPT Functions to Extract Text After a Character 6.

What is the use of substring in JavaScript?

The substring function helps in returning an atomic value not simple a node. The above function takes two inputs where the first string does search and the second string is the string to be searched for the first string.


3 Answers

If you're trying to parse some HTML code - it might be better to use HTMLAgilityPack.

But in the case it is just some set of strings you've obtained from somewhere and want to parse - you can also do it using regular expressions:

string s ="<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen></iframe>";
var match = Regex.Match(s, "src=\"(.*?)\"");
string src;
if (match.Success)
    src = match.Groups[1].Value;
like image 150
Andrey Korneyev Avatar answered Oct 16 '22 12:10

Andrey Korneyev


A naive implementation, in which I assume you have a string as input:

string input = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen></iframe>";

if (input.Contains("src=\""))
{
    string output = input.Substring(input.IndexOf("src=\"") + 5);
    // output is: https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe>

    output = output.Substring(0, output.IndexOf("\""));
    // output is: https://www.youtube.com/embed/KRFHiBW9RE8
}

It will certainly miss edge cases like src =", but will give you a place to start. Obviously this is also a problem which can be solved using regular expressions; I'll leave that for someone else to answer.

like image 20
pyrocumulus Avatar answered Oct 16 '22 10:10

pyrocumulus


I'd be tempted to split all the properties out into an array as it's possible I might want some of the others later on as well. In doing this it would also allow easy access to the 'src' property. So I'd do something like this:

string iFrameString = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen>";

//split properties based on spaces
string[] tagProps = iFrameString.Split(new Char[]{' '});

//get the property out.
string prop = "src=\"";
string source = Array.Find(tagProps, x => x.StartsWith(prop, StringComparison.InvariantCultureIgnoreCase));

string ModifiedSource = source.Substring(prop.Length,source.Length - prop.Length);

The benefit of this is that you have all the other properties in your array as well and you could get those out if needed.

like image 2
sr28 Avatar answered Oct 16 '22 10:10

sr28