Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Regular expressions, returning only desired data.

  • 19-11-2009 02:08PM
    #1
    Registered Users, Registered Users 2 Posts: 2,238 ✭✭✭


    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, Registered Users 2 Posts: 4,775 ✭✭✭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, Registered Users 2 Posts: 2,238 ✭✭✭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