Get Text Between 2 HTML Tags C#

Get text between 2 html tags c#

You could be incredibly specific about it:

var regex = new Regex(@"<span id=""point_total"" class=""tooltip"" oldtitle="".*?"" aria-describedby=""ui-tooltip-0"">(.*?)</span>");

var match = regex.Match(@"<span id=""point_total"" class=""tooltip"" oldtitle=""Note: If the number is black, your points are actually a little bit negative. Don't worry, this just means you need to start subbing again."" aria-describedby=""ui-tooltip-0"">31</span>");

var result = match.Groups[1].Value;

C# Get string between 2 HTML-tags

Use Regex:

 var a = @"<div class=""s20 red"">120.000.000 kr.</div>";
var b = Regex.Match(a, "(?<=>)(.*)(?=<)");
Console.WriteLine(b.Value);

Output:
120.000.000 kr.

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

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();

Extract string between html tags

Try m.Groups[1].Value (documentation for Groups), or m.Result("$1") (documentation for Result); either should work.

The object m which was returned by Regex.Match is an object that contains various pieces of information about what was matched. This includes both the entire string that was matched, including in this case the title tags themselves, and the parts of the string matched by each group of parentheses. m.Value gives the entire string; m.Groups[1].Value gives the part matched by the first group, m.Groups[2].Value gives the part matched by the second group, etc. This has to be done outside the regular expression because a program might want more than one group; for instance, if you're matching a time of day, like (\d+):(\d+), then you might want to assign the hours (m.Groups[1].Value) to one variable and the minutes (m.Groups[2].Value) to a different variable.

extract the text between two tag in C#

Here are the codez

var items = new List<string>();
foreach (Match match in Regex.Matches(text, "<m>(.*?)</m>"))
items.Add(match.Groups[1].Value);
string output = String.Join(" ", items);

someTextBox.Text = output;

if (items.Any())
anotherTextBox.Text = items[0];

if (items.Count > 2)
whateverTextBox.Text = items[3];

Get text between html tags and Not-between html tags into List

It's not really the best idea to do this task with regular expressions because of the time complexity, I guess. For example, we can check this expression,

(?<=>)([^<]*?)(?=<)|\b([^<>]{2,})

which is explained on the top right panel of this demo, if you wish to explore further, and in this link, you can watch how it would match against some sample inputs step by step, if you like, and how inefficient the process might be.

Example

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = @"(?<=>)([^<]*?)(?=<)|\b([^<>]{2,})";
string input = @"Lorem ipsum dolor sit <b>amet, consectetur adipiscing elit,</b>
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation <i>ullamco laboris
nisi ut</i> aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.";
RegexOptions options = RegexOptions.Singleline;

foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}

C# Demo



Related Topics



Leave a reply



Submit