开发者

C# regex question - help

开发者 https://www.devze.com 2023-02-17 13:24 出处:网络
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&

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"


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);


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

That should do it.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消