Filip Koper
Filip Koper

Reputation: 39

QRegExp html line

I had no problems with QRegExp class but now i have one. My Code

QRegExp re("d class=\\.green\\.>([+,0-9]+)<.td><td>[.0-9]+<.td><td>[.0-9]+<..n");
QString str=AliorLinia;

qDebug()<<str;
QStringList list;
int pos=0;

while((pos=re.indexIn(str, pos))!=-1){
    list << re.cap(1);
    pos+=re.matchedLength();
}

qDebug()<<list;

console show me

enter image description here

,line of str is on screen

console should show +0,10 but it show only () why :(?

Upvotes: 1

Views: 75

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627327

You do not get a match because your real literal string looks like

d class="green">+0.10</td><td>69.7</td><td>69.0</
<NEWLINE>

The \" and \n you see in the debugger are just telling you the string literal contains a literal double quote and a newline symbol (a char with decimal code of 10).

Also, your number contains a ., not , as a decimal separator, so you must add it to the [+,0-9] character class.

So, your regex with minimal amendments can look like

QRegExp re("d class=.green.>([+.,0-9]+)<.td><td>[.0-9]+<.td><td>[.0-9]+<.\n");

See the regex demo

Upvotes: 1

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

Because your regex pattern requires the green attribute to be surrounded by dots literally - this is what \\. stands for in a quoted string.

The regex you really want here is \\. (i.e. a backslash literally followed by any character), that's right. But in a C string literal each of these two backslashes should be escaped by an extra slash:

QRegExp re("d class=\\\\.green\\\\.>([+,0-9]+)<.td><td>[.0-9]+<.td><td>[.0-9]+<..n");
                    ^ ^       ^ ^
                 add these backslashes

Upvotes: 0

Related Questions