[SOLVED]Getting a substring from a start character



  • I have a string generated by a low level intrinsic function which when run displays the CPU ID string. The string displayed is:

    Intel(TM) i5 CPU 750 <at_symbol> 2.5GHz

    substitute the real symbol for <at_symbol>

    I would like to parse the string so when this program is run, I can always get the CPU speed. If I use a split function and build a QStringList I can not guarantee that the string will have the same number of tokens.
    I prefer to parse the string such that I search for the at_symbol and extract the next three characters. That is, in the string above, grab the 2.5 which follows the at_symbol and the space. Is a combination of one (or more) of the QString methods plus a QRegEx expression needed?


  • Lifetime Qt Champion

    Hi,

    Depending on your version of Qt you should rather use QRegularExpression.

    Something like:

    @"@\s+((\d+)(\.\d+)?)"@

    should do what you want



  • Qt 5.1.1.

    So then with the expression string, that serves to isolate ALL that follows? Does it include the "at" symbol as well? I want to grab 2.5 from the original string. Some machines return different length strings so I can not guarantee outcome from an index of a QStringList if that makes sense.

    Thanks


  • Lifetime Qt Champion

    The length of the string doesn't matter. This expression searches for an at followed by one or more whitespace(s) then one or more number(s) optionally followed by a dot and again one or more number(s). The first capture will get you the full number



  • My attempt:

    @
    NameOut = "Intel(R) Core(TM) i5 CPU 750 '@' 2.67GHz"
    @

    @
    QStringList tokensActual = NameOut.split(" ",QString::SkipEmptyParts);
    double cpuSpeed = tokensActual.at(6).left(3).toDouble();
    @

    I had to put the at symbol in single quote to print properly - that's not how it is actually displayed.

    The above works but can not rely on the string NameOut having the same number of tokens - don't want fixed indexes!

    What you suggested:

    @
    QRegularExpression cpuString("@\s+((\d+)(\.\d+)?)");
    QRegularExpressionMatch match = cpuString.match(NameOut);
    cpuSpeed = match.captured(1).toDouble();
    @

    Yields the numeric value 2.67. How is this any different from splitting a string into a string list?

    BTW, it results in the second capture, not the first.


  • Lifetime Qt Champion

    The regular expression catches what you are looking for and in this case captures what you would like to get for an easy retrieval. The expression could be a bit refined in the sense that if your string always ends in the same manner you can add that parameter to the regexp thus no matter what comes before the at, you always get your number.

    Splitting a string gives you a list of of all elements separated by the given separator. Now you have to parse or know the exact position of your number to get it



  • [quote author="SGaist" date="1423524922"]The regular expression catches what you are looking for and in this case captures what you would like to get for an easy retrieval. The expression could be a bit refined in the sense that if your string always ends in the same manner you can add that parameter to the regexp thus no matter what comes before the at, you always get your number.

    Splitting a string gives you a list of of all elements separated by the given separator. Now you have to parse or know the exact position of your number to get it[/quote]

    OK, thanks. I think this issue went in a circle though. Perhaps as you mentioned the regular expression needs refinement. I wanted to NOT depend on an index (hard coded values are risky) of a list, but rather search for and extract the proper sub-string



  • [quote author="SGaist" date="1423524922"]The expression could be a bit refined in the sense that if your string always ends in the same manner you can add that parameter to the regexp thus no matter what comes before the at, you always get your number.[/quote]

    The string fortunately always ends the same. I'm not understanding what you mean by a parameter added to the regexp to always get the number without the at symbol and without the suffix (GHz).

    Thanks.


  • Lifetime Qt Champion

    You can use

    @"@\s+((\d+)(\.\d+)?)GHz$"@

    or

    @"@\s+((\d+)(\.\d+)?)[a-zA-Z]+$"@

    If you have a match the capture order is always the same. So you can safely use an index.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.