PDA

View Full Version : Capture text from changeable source (regex)



Alir3z4
5th December 2011, 06:06
Actually i couldn't find suitable title than this!
Okay i have a QTextBrowser which get output from QProcess and the output printed-out from "axel" downloader.
What i want to do is get just proggress text which is like [ #%]
the output is here


[ 0%] [0 ] [ 275.9KB/s] [ 2h06]
[ 22%] [0 ] [ 276.4KB/s] [ 2h06]
[ 34%] [0 ] [ 276.9KB/s] [ 2h06]
[ 43%] [0 ] [ 278.9KB/s] [ 2h05]

in try some regex pattern which i was failed in them.

\\d%
\\d{1,3}%

so if i can get the progress text then i can reach to download speed, remaining time and others i think.

i just want to show the download progress on the gui, of course i can calculate the download progress by orignal file size and downloaded size, but get help from regex seems more clever

Lykurg
5th December 2011, 08:42
Please read the documentation to QRegExp. As a short kick-off
QRegExp rx("\\[[ ]*([1-9]{1,2})\\%\\]");

Alir3z4
5th December 2011, 14:26
Despite that yours didn't smash any stone for me :|, Well thanks who move this thread to Newbie sub-forum because it remind me that don't hack stupid soft sands when you can use API :D
i just found axel's API.

Lykurg
5th December 2011, 17:19
Using the API of your downloader is surely the best way to go. As to my suggestion:
QString test = "[ 34%] [0 ] [ 276.9KB/s] [ 2h06]";
QRegExp rx("\\[[ ]*([1-9]{1,2})\\%\\]");
rx.indexIn(test);
qWarning() << rx.capturedTexts();returns 34. The percentage you wanted... (At least I understand your post so.)