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] QString and regular expressions
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QString and regular expressions

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 32.1k 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 30 May 2013, 20:35 last edited by
    #1

    When I call QString::contains on the following string "\?\USB#VID_0FE2&PID", I always return false. If I just use "VID_0FE2" as the string constant, I can get the function to return true. I do however need the prior form of the string. Any way to resolve this? Is it that I need to use a regular expression?

    Thanks in advance.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 May 2013, 20:58 last edited by
      #2

      Hi,

      If you want to use the regex variant of contains, you'll need to do this:

      @myString.contains(QRegExp(“\?\USB#VID_0FE2&PID”))@

      Note: you can use the regex demo in Qt's source to ensure the one you use will work as expected, it also provide the "escaped" version you need to put in your code.

      Hope it helps

      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
      • S Offline
        S Offline
        stereomatching
        wrote on 30 May 2013, 21:09 last edited by
        #3

        If you are using Qt5, QRegularExpression should be a better choice

        "QRegularExpression":http://dangelog.wordpress.com/2012/04/07/qregularexpression/

        1 Reply Last reply
        0
        • A Offline
          A Offline
          astodolski
          wrote on 31 May 2013, 11:50 last edited by
          #4

          [quote author="SGaist" date="1369947498"]Hi,

          If you want to use the regex variant of contains, you'll need to do this:

          @myString.contains(QRegExp(“\?\USB#VID_0FE2&PID”))@

          Note: you can use the regex demo in Qt's source to ensure the one you use will work as expected, it also provide the "escaped" version you need to put in your code.

          Hope it helps[/quote]

          Thanks anyway, it doesn't work. I still return a false and I'm using Qt 4.8.4.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 31 May 2013, 20:11 last edited by
            #5

            You are not stating clearly your string issu, can you show

            • what string you are getting
            • what regex you are using
            • what result you are expecting

            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 31 May 2013, 20:57 last edited by
              #6

              Im trying to see if a USB descriptor contains the VID that I am looking for. I search all connected devices and what's returned are similar to the literal that I posted. As I stated before, it works if I leave out the escape characters and just use “VID_0FE2”. I am trying to get a true condition for the contains method. Perhaps the regex string used is wonky and don't have a grasp on forming the regex string.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 31 May 2013, 21:07 last edited by
                #7

                So you need something like this ?

                @"VID_[a-zA-Z0-9]{4}"@

                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 3 Jun 2013, 11:44 last edited by
                  #8

                  My code:

                  @
                  QString qs = QString::fromUtf16(DetailData->DevicePath);
                  qs = qs.toUpper();

                      if(qs.contains(QRegExp("VID_[a-zA-Z0-9]{4}")))
                      {
                          qDebug() << qs << " contains  the USB vendor ID";
                  
                          if (deviceInfo)
                          {
                              SetupDiDestroyDeviceInfoList(deviceInfo);
                              break;
                          }
                      }
                  

                  @

                  Function returns true on the first pass which is wrong:

                  @
                  "\?\usb#vid_0e0f&pid_0003#6&b77da92&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"
                  @

                  I need it true for VID_0FE2

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TheBadger
                    wrote on 3 Jun 2013, 12:27 last edited by
                    #9

                    I would also like to help but to re-iterate:
                    [quote author="SGaist" date="1370031066"]You are not stating clearly your string issu, can you show

                    • what string you are getting

                    • what regex you are using

                    • what result you are expecting
                      [/quote]

                    • So what is the output of qs after line 2.

                    If it is the line in your second code block then your RegExp will not work to match the start since the backslash is a special command in RegExps, you need something like (escape the slashes):
                    @
                    QRegExp(“\\?\usb#vid_[a-zA-Z0-9]{4}&pid”)
                    @

                    (note: using modified version using comment from SGaist)

                    Note: tThat is for if you keep the string lower case (not do line 2 on the string). If you still want to do toUpper(), replace the lower case letters with uppercase in the pattern.

                    Use the Regular Expression Example to evaluate such 'simple' patters, the "Escaped Pattern" is then the version that you put in the code.


                    Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TheBadger
                      wrote on 3 Jun 2013, 12:32 last edited by
                      #10

                      I went through your question again, if you are only interested if it is "VID_0FE2" then just escape the start of your original pattern with what I have shown:
                      @
                      QRegExp("\\?\USB#VID_0FE2&PID")
                      @


                      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        astodolski
                        wrote on 3 Jun 2013, 13:29 last edited by
                        #11

                        [quote author="Badger" date="1370262432"]I would also like to help but to re-iterate:
                        [quote author="SGaist" date="1370031066"]You are not stating clearly your string issu, can you show

                        • what string you are getting

                        • what regex you are using

                        • what result you are expecting
                          [/quote]

                        • So what is the output of qs after line 2.

                        If it is the line in your second code block then your RegExp will not work to match the start since the backslash is a special command in RegExps, you need something like (escape the slashes):
                        @
                        QRegExp(“\\?\usb#vid_[a-zA-Z0-9]{4}&pid”)
                        @

                        (note: using modified version using comment from SGaist)

                        Note: tThat is for if you keep the string lower case (not do line 2 on the string). If you still want to do toUpper(), replace the lower case letters with uppercase in the pattern.

                        Use the Regular Expression Example to evaluate such 'simple' patters, the "Escaped Pattern" is then the version that you put in the code. [/quote]

                        The output after line 2 is just the upper case version of what I posted previously:

                        @"\?\USB#VID_0E0F&PID_0003#6&B77DA92&0&1#{A5DCBF10-6530-11D2-901F-00C04FB951ED}"@

                        I suspect it was the RegExp string that wasn't working correctly. The modified version based you posted upon the suggestion from SGaist did not work either. What is shown to work is the call using:

                        @
                        if(qs.contains(QRegExp("\\?\USB#VID_0FE2&PID")))
                        @

                        Thanks to both of you for the help.

                        1 Reply Last reply
                        0

                        1/11

                        30 May 2013, 20:35

                        • Login

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