Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
Thread Tools Search this Thread
19-11-2009, 13:08   #1
techguy
Registered User
 
Join Date: Jun 2004
Location: Dublin
Posts: 1,970
Send a message via Skype™ to techguy
Regular expressions, returning only desired data.

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:
Code:
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.
techguy is offline  
Thanks from:
Advertisement
19-11-2009, 21:52   #2
JohnK
Subscriber
 
JohnK's Avatar
 
Join Date: Dec 2002
Location: Cork
Posts: 4,267
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:
Code:
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
JohnK is offline  
Thanks from:
19-11-2009, 22:52   #3
techguy
Registered User
 
Join Date: Jun 2004
Location: Dublin
Posts: 1,970
Send a message via Skype™ to techguy
Quote:
Originally Posted by JohnK View Post
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:
Code:
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.
techguy is offline  
Thanks from:
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet