Hack Forums
[C#] Regex problem - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Regex problem (/showthread.php?tid=5036284)



Regex problem - dark.team - Oct 27, 2015

I made the following regex to grab the % from a string.
Code
string s =<tr><td colspan="4"><b>Sort tonerkassett  ~81%</b></td></tr><tr>

Regex expression = new Regex("(?=~).*(?=</b></td><)");
Match result = expression.Match(s);

I want to grab that 81 but the above regex code giving me ~81 how can grab only 81.

Thanks


* - Odus - Oct 27, 2015

This happens when you're lazy with the capture tokens! Try this pattern:
Code
(\d{1,2})%

It doesn't account for decimals though.


* - LUOIdeRentA - Oct 27, 2015

I would of done this:

Code
~(.*?)%

but, Odus' way is better as it shows what you're capturing would be only numbers, whereas I was kinda lazy with mine.


* - dark.team - Oct 27, 2015

Thanks a lot my issue is solved i am not good in regex. :)