0

This is my program:

 class Program
    {
        private static Regex _regex = new Regex("<span id='TotalG'>$ (?<amount>.*?)</span>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);

        static void Main(string[] args)
        {

            string text = File.ReadAllText("file.txt");

            var match = _regex.Match(text);

            var group = match.Groups["amount"];

          //  Console.ReadKey();

        }
    }

The text does contain this text:

<td align="right" bgcolor=c0c0c0 style="font-weight:bold"> <span id='TotalG'>$ 0.00</span></td>
        <td>&nbsp;</td></tr>

yet the named group is always empty. Any idea why would this happen?

1
  • your regex doesn't even match,
    – Dave Bish
    Jun 6, 2013 at 9:38

2 Answers 2

3

Escape the $, < and > by adding a backslash before it.

And I would actually suggest a different regex for the named group as well:

"\\<span id='TotalG'\\>\\$(?<amount>[^\\<]*)\\</span\\>"

I'm pretty sure that >'s and <'s should only be escaped when in (parenthesis), but I rather escape stuff instead of remembering regex rules.

0
0

If you are processing html you can use help of Html agile pack

var doc = new HtmlDocument();
doc.Load("test.html");
var node = doc.DocumentNode.SelectSingleNode("//span[@id='TotalG']");
if (node != null)
{
    var temp = node.InnerText;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.