1

I have a html string which i'm parsing which looks like below. I need to get the value of @Footer.

strHTML = "<html><html>\r\n\r\n<head>\r\n<meta http-equiv=Content-Type 
           content=\"text/html; charset=windows-1252\">\r\n
           <meta name=Generator content=\"Microsoft Word 14></head></head><body> 
           <p>@Footer=CONFIDENTIAL<p></body></html>"

I have tried the below code, how do i get the value?

Regex m = new Regex("@Footer", RegexOptions.Compiled);
foreach (Match VariableMatch in m.Matches(strHTML.ToString()))
{
     Console.WriteLine(VariableMatch);
}

5 Answers 5

2

You need to capture the value after the =. This will work, as long as the value cannot contain any < characters:

Regex m = new Regex("@Footer=([^<]+)", RegexOptions.Compiled);
foreach (Match VariableMatch in m.Matches(strHTML.ToString()))
{
    Console.WriteLine(VariableMatch.Groups[1].Value);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can do this with regex, but it's not necessary. One simple way to do this would be:

var match = strHTML.Split(new string[] { "@Footer=" }, StringSplitOptions.None).Last();
match = match.Substring(0, match.IndexOf("<"));

This assumes that your html string only has one @Footer.

Comments

1

Your regex will match the string "@Footer". The value of the match will be "@Footer".

Your regex should look like this instead :

Regex regex = new Regex("@Footer=[\w]+");
string value = match.Value.Split('=')[1];

Comments

1

Use a matching group.

Regex.Matches(strHTML, @"@Footer=(?<VAL>([^<\n\r]+))").Groups["VAL"].Value;

Comments

0

If that's all your string, we can use string methods to solve it without touching regex stuff:

var result = strHTML.Split(new string[]{"@Footer=", "<p>"}, StringSplitOptions.RemoveEmptyEntries)[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.