Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Getting a substring from a start character
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Getting a substring from a start character

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astodolski
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Something like:

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

      should do what you want

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        astodolski
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            astodolski
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                astodolski
                wrote on last edited by
                #7

                [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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  astodolski
                  wrote on last edited by
                  #8

                  [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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved