Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract a string between <strong> tags usings C#?

Tags:

c#

regex

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.

like image 890
Bazinga Avatar asked Jun 25 '13 13:06

Bazinga


5 Answers

Given your example, a simple Split would do it, e.g.

var innerText = text.Split('>')[1].Split('<')[0];
like image 169
George Johnston Avatar answered Sep 18 '22 08:09

George Johnston


How about using a simple regular expression like:

(?<=<strong>).+?(?=<)
like image 37
Brandon Avatar answered Sep 21 '22 08:09

Brandon


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();
like image 23
Anna.P Avatar answered Nov 20 '22 06:11

Anna.P


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.

like image 14
Soner Gönül Avatar answered Nov 20 '22 05:11

Soner Gönül


Have a look at the HTML Agility Pack or AngleSharp

like image 8
devdigital Avatar answered Nov 20 '22 05:11

devdigital