0

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"

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Just because you aren't using Regexes. (RANT MODE ON) I'm quite tired of persons that don't know how to compose Regexes and try to use them even to go to the bathroom... I've programmed for more than 10 years before learning Regexes and I was always happy with strstr, strcpy... Why now everyone wants to use regexes?
I want regex. It looks nicer.
@Hooch If you want nicer take a Mac. Code must be maintainable. If you can't write Regexes, you don't write Regexes.
3
Match m = Regex.Match(string, @"Transfer: ([0-9.]+) MB");
if (m.Success)
{
    string remaining = m.Groups[1].Value
}

That should do it.

Comments

0
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.

Comments

-1
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

1 Comment

My guess is: you are not actually capturing the number, and MB is not at the end of the string (so do not use $).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.