1

I want use regex to find something in string (or QString) that is between " (quotes).

My simple String: x="20.51167" and I want 20.51167.

Is it possible with Regular Expressions ??

On start I had somthing like this string :

<S id="1109" s5="1" nr="1183" n="Some text" test=" " x="20.53843" y="50.84443">

Using regexp like: (nr=\"[0-9]+\") (y=\"[0-9 .^\"]+\")" etc I get my simple string like x="20.51167". Maybe this is wrong way and I can get value that is between quotes at one time ??

poplawskidaniel
  • 55
  • 1
  • 10

5 Answers5

2

Try this works. untested

="([^"]+)"

The above captures anything that is in-between =" "

tuxuday
  • 2,977
  • 17
  • 18
2

For your particular example, this will work:

#include <QRegExp>
#include <QString>
#include <iostream>
int main()
{
    //Here's your regexp.
    QRegExp re("\"[^\"^=]+\"");
    //Here's your sample string:
    QString test ="<S id=\"1109\" s5=\"1\" nr=\"1183\" n=\"Some text\" test=\" \" x=\"20.53843\" y=\"50.84443\">";
    int offset = 0;
    while( offset = re.indexIn( test, offset + 1 ) )
    {
        if(offset == -1)
            break;
        QString res = re.cap().replace("\"", "");
        bool ok;
        int iRes;
        float fRes;
        if( res.toInt( &ok ) && ok )
        {
            iRes = res.toInt();
            std::cout << "int: " << iRes << std::endl;
        }
        else if ( res.toFloat( &ok ) && ok )
        {
            fRes = res.toFloat();
            std::cout << "float: " << fRes << std::endl;
        }
        else
            std::cout << "string: " << res.toStdString() << std::endl;
    }
}

The output will be;

int: 1109
int: 1
int: 1183
string: Some text
string:  
float: 20.5384
float: 50.8444
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • Thanks. But the output still has quotes. My next step will be changing output to strings or ints or doubles and put these to database. So I need output without quotes and I need to know what is what (that is the reason that I made different regexp for each one. – poplawskidaniel Jul 13 '12 at 10:29
  • @poplawskidaniel, It has quotes because it's a list of QStrings. Convert them to appropriate types with QString's methods (e.g. `toFloat()`) methods, and you're done. – SingerOfTheFall Jul 13 '12 at 10:30
  • It has quotes inside, so converting using `ToInt()`, `ToFloat()` etc not working (always return 0). Try it. – poplawskidaniel Jul 13 '12 at 10:59
  • it's not exacly what I want but it's effective. Thanks – poplawskidaniel Jul 13 '12 at 12:21
1

In this expression : (nr=\"[0-9]+\") (y=\"[0-9 .^\"]+\")" Delete the last quote after )
For your regular expression try :

x=^[0-9]+.[0-9]{5}

Angelika
  • 381
  • 1
  • 9
  • 22
0

If you want to find anything in quotes, I guess the regex should read:

"([^"]*)"

(anything that is not a quote between quotes)

Andrzej
  • 5,027
  • 27
  • 36
0

You just have to move your capturing group inside the quotes:

x=\"([0-9.]+)\"
Stefan
  • 109,145
  • 14
  • 143
  • 218