Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Regular expressions, returning only desired data.

  • 19-11-2009 1:08pm
    #1
    Registered Users Posts: 2,228 ✭✭✭


    Hi,

    I've been playing around with a regular expressions in C# for a while now. I've been successfull in creating simple expressions but grabbing the desired data from my matches is still a little hacky.

    For example, take the following expression:
    Downloaded: </B></TD><TD ALIGN=RIGHT>&nbsp;(\d+) MB</TD>
    

    This will return the whole string that matches this expression. However, I only want the number (just before MB) in this string to be returned.

    Up to now i've been working with the the whole string that is returned and using the SubString() function to extract the value. The obvious problem here is that I must know the number of digits in the value.

    So does anybody here work with regular expressions and know how to accomplish this? I've been reading up on MSDN .NET about groups in regular expression but i'm not sure this is suitable either/it may be too much work.

    Thanks.


Comments

  • Registered Users Posts: 4,780 ✭✭✭JohnK


    Groups would be exactly what you want here and they're very easy to use. In VB.Net I'd do this so I'd say C# would be similar enough:
    Dim sFullText As String = "Downloaded: </B></TD><TD ALIGN=RIGHT>&nbsp;230.44 MB</TD>"
    Dim sExpression As String = "Downloaded: </B></TD><TD ALIGN=RIGHT>&nbsp;([0-9.]+?) MB</TD>"
    Dim oRegEx As New Regex(sExpression)
    For Each mMatch As Match In oRegEx.Matches(sFullText)
        ' Either loop through all groups
        For Each oGroup As Group In mMatch.Groups
    	MsgBox(oGroup.Value)
        Next
    
        ' Or jump straight to Group 1 if it exists
        If mMatch.Groups.Count >= 1 Then MsgBox(mMatch.Groups(1).Value)
    Next
    
    In this case it'll give a messagebox with the entire match first, then the specific group we're looking for second. Alternativly you could jump directly to the index of the group you want


  • Registered Users Posts: 2,228 ✭✭✭techguy


    JohnK wrote: »
    Groups would be exactly what you want here and they're very easy to use. In VB.Net I'd do this so I'd say C# would be similar enough:
    Dim sFullText As String = "Downloaded: </B></TD><TD ALIGN=RIGHT>&nbsp;230.44 MB</TD>"
    Dim sExpression As String = "Downloaded: </B></TD><TD ALIGN=RIGHT>&nbsp;([0-9.]+?) MB</TD>"
    Dim oRegEx As New Regex(sExpression)
    For Each mMatch As Match In oRegEx.Matches(sFullText)
        ' Either loop through all groups
        For Each oGroup As Group In mMatch.Groups
    	MsgBox(oGroup.Value)
        Next
    
        ' Or jump straight to Group 1 if it exists
        If mMatch.Groups.Count >= 1 Then MsgBox(mMatch.Groups(1).Value)
    Next
    
    In this case it'll give a messagebox with the entire match first, then the specific group we're looking for second. Alternativly you could jump directly to the index of the group you want

    Perfect, thanks.. I'll give that a go later.


Advertisement