Hooch
Hooch

Reputation: 29673

C# regex question - help

How can I get MB of transfer left from this string?

<div class="loggedIN"> 
Jesteś zalogowany jako: <a href="konto" style="color:#ffffff;"><b>Hooch</b></a> <i>|</i> <a style="font-size:11px;" href="wyloguj">wyloguj się </a><br/><br/> 
Transfer: 21410.73 MB<br/><span title="Dziennie limit ściąganie z RS zwiększany jest o 500MB, kumuluje się maksymalnie do 2500MB. Podczas pobierania z RS, limit jest obniżany a transfer pobierany jest z konta podstawowego.">z czego max 2500 MB na RS <a href="konto,rs"><img src="style/img/ico/info_icon.png" border="0"/></a></span><br/> 
</div>

I don't know how regex have look to get one group with this: "21410.73"

Upvotes: 0

Views: 107

Answers (4)

WorldIsRound
WorldIsRound

Reputation: 1554

Match match = Regex.Match(string, @"Transfer: [0-9.]+ MB$");
if (match.Success)
   var dataLeft = match.Groups[1].Value;

I often use this online tester for my reg-ex tests : http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

Upvotes: -1

Guffa
Guffa

Reputation: 700342

Using regular expression for that seems overkill. Just look for the string "Transfer: " and then look for the following " MB", and get what's between:

int start = str.IndexOf("Transfer: ") + 10;
int end = str.IndexOf(" MB", start);
string mb = str.Substring(start, end - start);

Upvotes: 4

xanatos
xanatos

Reputation: 111860

var rx = new Regex(@"Transfer: ([0-9]+(?:\.[0-9]+)?)");
var result = rx.Match(html).Groups[1].Value;

BUT you shouldn't parse html with Regex. I'm "anchoring" the regex to the "Transfer: " string.

Upvotes: 0

CanSpice
CanSpice

Reputation: 35788

Match m = Regex.Match(string, @"Transfer: ([0-9.]+) MB");
if (m.Success)
{
    string remaining = m.Groups[1].Value
}

That should do it.

Upvotes: 3

Related Questions