Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

How to get the value from in between tags?

Last post 06-04-2008, 8:06 PM by hifiger2004. 4 replies.
Sort Posts: Previous Next
  •  06-04-2008, 4:32 AM 42881

    How to get the value from in between tags?

    Hi Guys,

    I am just new to Regular Expressions. I am having problem pulling out the string value between tags.

    How can I get the text "Your Payment:" and "300.00"?

    I am using ASP.Net 2.0 and I haven't tried using the regex before. What is the correct pattern to apply here?

    Thanks

     

    Sample source:

    <tr>

    <td class="fieldLabel" style='width: 150px; height: 13px;' valign='top'>

    Your Payment:

    </td>

    <td class="fieldValue" style='height: 13px'>

    300.00

    </td>

    </tr>

    Filed under:
  •  06-04-2008, 10:33 AM 42888 in reply to 42881

    Re: How to get the value from in between tags?

    hifiger2004,

    If I were you, I wouldn't use regular expressions at all.  Dealing with HTML can be pretty messy, and if your input is really as simple as the example you provide here, you could easily do what you need to do with a System.Xml.XmlDocument:

    XmlDocument xmlDocument = new XmlDocument();

    xmlDocument.LoadXml( text );

    XmlNodeList nodes = xmlDocument.SelectNodes( "tr/td" );

    string first = nodes.Item( 0 ).InnerText.Trim();
    string second = nodes.Item( 1 ).InnerText.Trim();

    This would be much less prone to error, and much easier to maintain.

    Jeff
     

  •  06-04-2008, 12:54 PM 42895 in reply to 42888

    Re: How to get the value from in between tags?

    Hi Jeff,

     I would like to try your advice. But sorry to tell you I haven't tried this xml code thing yet.

    I would like to ask a favor, can you send me the continuation of your program if how can I determine if the pointer is pointing on the node "Your Payment:" and node "300.00". Because in my real html file it contains different nodes and two of these nodes could be "Your Payment:" and "Your discount:" I am thinking of doing it inside a loop statment. Please see example below if how will I check the node one by one if there are 20 nodes that I need to check.

    Thank you

     For example:

    <tr>

     </td>

    <td class ="fieldlabel" style='width; 150px; height: 13px;'valign='top'>

    Your Payment:

    </td>

    <td class="fieldValue" style=height 13px'>

    300.00

    <td>

    <td class="fieldLabel" style='width': 150px;height: 13px;'valign='top'>

    Your Discount:

    </td>

    <td class="fieldvalue" style='height': 13px'>

    0.05

    </td>

    </tr>

  •  06-04-2008, 7:06 PM 42902 in reply to 42895

    Re: How to get the value from in between tags?

    This is where you might want to try regular expressions.  If the payment or discount is always right after the "Your Payment" or "Your discount" node, you could do this (I just typed this in here, so the syntax might not be exactly right):

    XmlNodeList nodes = xmlDocument.SelectNodes( "tr/td" );

    for ( int i = 0; i < nodes.Count; i++ )
    {
        if ( Regex.Match( nodes.Item( i ).InnerText.Trim(), "Payment", RegexOptions.IgnoreCase ).Success )
        {
            payment = nodes.Item( ++i ).InnerText.Trim();
        }
        else if ( Regex.Match( nodes.Item( i ).InnerText.Trim(), "Discount", RegexOptions.IgnoreCase ).Success )
        {
            discount = nodes.Item( ++i ).InnerText.Trim();
        }

    Jeff

  •  06-04-2008, 8:06 PM 42907 in reply to 42902

    Re: How to get the value from in between tags?

    Hi Jeff,  please disregard my reply prior to this.

    Thank you. But I got an error: I follow your code and I converted it to vb coding. Please check below my sample html code

    Data at the root level is invalid. Line 1, position 1.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
    ==========================================================================================================
     

    Below are my sample html source

    SAMPLE HTML SOURCE (Online.txt):

    <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=utf-8">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Current Payment</title>
    <style >
    body {
     font-family: Verdana,arial,helvetica,sans-serif;
     font-size: 10px;
     color: Black;
     text-decoration : none;
    }
    .sectionLabel
    {
       font-family: Verdana,arial,helvetica,sans-serif;
     font-size: 12px;
     font-weight: bold;
     color: Navy;
     text-decoration : none; 
    }
    .fieldLabel
    {
       font-family: Verdana,arial,helvetica,sans-serif;
     font-size: 10px;
     font-weight: bold;
     color: Green;
     text-decoration : none; 
    }
    .fieldValue
    {
       font-family: Verdana,arial,helvetica,sans-serif;
     font-size: 10px;
     font-weight: bold;
     color: #FF0099;
     text-decoration : none; 
    }
    </style>   
    </head>
    <body>
       
        <table width="100%">
    <tr>

     </td>

    <td class ="fieldlabel" style='width; 150px; height: 13px;'valign='top'>

    Your Payment:

    </td>

    <td class="fieldValue" style=height 13px'>

    300.00

    <td>

    <td class="fieldLabel" style='width': 150px;height: 13px;'valign='top'>

    Your Discount:

    </td>

    <td class="fieldvalue" style='height': 13px'>

    0.05

    </td>

    </tr>
        </table>
    </body>
    </html>

    ========================================================================================================= 

View as RSS news feed in XML