Say I have a string such as below:
"Unneeded text <strong>Needed Text</strong> More unneeded text"
How can I extract only the "Needed Text"? I'm guessing Regex is likely the simplest way but Regex still looks like hieroglyphics to me.
Given your example, a simple Split
would do it, e.g.
var innerText = text.Split('>')[1].Split('<')[0];
How about using a simple regular expression like:
(?<=<strong>).+?(?=<)
Regex regex = new Regex("<strong>(.*)</strong>");
var v = regex.Match("Unneeded text <strong>Needed Text</strong> More unneeded text");
string s = v.Groups[1].ToString();
You don't need regex for that.
You can use String.SubString
and String.Split
methods like;
string s = "Unneeded text <strong>Needed Text</strong> More unneeded text";
Console.WriteLine(s.Substring(s.IndexOf("<strong>") + "<strong>".Length, s.IndexOf("</strong>") - s.IndexOf("<strong>") - "<strong>".Length));
Output will be;
Needed Text
Here a DEMO.
Have a look at the HTML Agility Pack or AngleSharp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With