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] regex not working in qt

[Solved] regex not working in qt

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.9k Views
  • 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
    ashishbansal
    wrote on last edited by
    #1

    Hello everyone,
    I am trying to use regex in qt. I want to use regex for matching URLs. I am using this regex : http://regexr.com/3acbl
    But I got warnings and it didn't work :
    @
    warning: unknown escape sequence: '/'
    in QString regexp = "(http(s)?://.)?(www.)?[-a-zA-Z0-9@:%.+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%+.~#?&//=]*)";
    ^
    warning: unknown escape sequence: '/'
    warning: unknown escape sequence: '.'
    warning: unknown escape sequence: '+'
    warning: unknown escape sequence: '.'
    warning: unknown escape sequence: '+'
    @

    When I googled these, I got to know that c++ does not recognizes these as escape sequence and require one extra backslash "" there.
    @
    QString regexp = "(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%.\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%\+.~#?&//=]*)";
    @
    Although warnings went after using one extra backslash but it is still not matching URLs.

    Regards,
    Ashish Bansal

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      It looks like you should have a look to the documentation of "QRegExp":http://doc.qt.io/qt-5/qregexp.html and "QRegularExpression":http://doc.qt.io/qt-5/qregularexpression.html or the "regexp example":http://doc.qt.io/qt-5/qtwidgets-tools-regexp-example.html

      Vote the answer(s) that helped you to solve your issue(s)

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

        Hi,

        Out of curiosity, why the regexp ? Wouldn't QUrl be more adequate ?

        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
          ashishbansal
          wrote on last edited by
          #4

          Hi SGaist,

          I am not able to validate URI using QUrl::​isValid(). It does not checks the syntax of the URL. It just checks the encoding errors like if I my URL is
          @
          http://foo.bar/%3
          @

          It returns false and if URL is
          @
          foo://bar or foo://bar/baz
          @

          It returns true.

          Then How can I use it to validate the URL?

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

            Hi koahnig,

            Can you please explain it a bit?

            EDIT:
            @
            The C++ compiler transforms backslashes in strings. To include a \ in a regexp, enter it twice, i.e. \. To match the backslash character itself, enter it four times, i.e. \\.
            @

            I already have corrected this. Then where's the mistake?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              [quote author="ashishbansal" date="1423288352"]Hi koahnig,

              Can you please explain it a bit?

              EDIT:
              @
              The C++ compiler transforms backslashes in strings. To include a \ in a regexp, enter it twice, i.e. \. To match the backslash character itself, enter it four times, i.e. \\.
              @

              I already have corrected this. Then where's the mistake?[/quote]

              Your post requires quite a number of assumptions what you are trying to do.My impression was that you simply trying to store some syntax of regexp in a QString. However, you would need an engine such as QRegExp or QRegularExpressions.
              There you should find also all things for C++ string handling.Therefore I thought that you should know about QRegExp.

              Vote the answer(s) that helped you to solve your issue(s)

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

                Well for the sake of simplicity, I hadn't posted the whole code. I am already using QRegExp in my code.
                @
                QString url = "http://foo.bar";

                QString regexp = "(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%.\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%\+.~#?&//=]*)";

                QRegExp rx(regexp);
                rx.indexIn(url);

                if(rx.cap(0).length() != 0){
                //url passed validation test
                ....
                }
                @

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  OK, sorry for my wrong assumption.

                  I would recommend the QRegExp example for checking, if you are not doing this already. I am using it for fast checks of reg exp.

                  I have tried with your example, but I could not verify any of your examples. The exampel gives you also the string with proper escape sequences for your code. However, I had to reverse your escapes. Probably there is the problem.

                  Vote the answer(s) that helped you to solve your issue(s)

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

                    "This":https://gist.github.com/dperini/729294 might interest you then

                    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
                      ashishbansal
                      wrote on last edited by
                      #10

                      [quote author="koahnig" date="1423307221"]OK, sorry for my wrong assumption.

                      I would recommend the QRegExp example for checking, if you are not doing this already. I am using it for fast checks of reg exp.

                      I have tried with your example, but I could not verify any of your examples. The exampel gives you also the string with proper escape sequences for your code. However, I had to reverse your escapes. Probably there is the problem.
                      [/quote]

                      Hey thanks for referring to that tool. Actually I missed one backslash on "\b" , that's why it was not working but that tool gave me correct escaped string on passing my normal js regex :)

                      [quote author="SGaist" date="1423344819"]"This":https://gist.github.com/dperini/729294 might interest you then[/quote]

                      Well that regex is most popular regex on the Google but I don't know why it does not work for me. You can check it here : http://regexr.com/3acgt
                      Anyway Thanks!

                      [In case someone needs regex for URL, you can try this out]
                      Finally I have used this basic regex for URL validation:
                      @
                      ^(((http|ftp)(s?)://)|(www.))(([a-zA-Z0-9-.]+(.[a-zA-Z0-9-.]+)+)|localhost)(/?)([a-zA-Z0-9-.?,'/\+&%$#_])?([\d\w./%+-=&?:\"',|~;])$
                      @

                      Escaped Pattern for using it in C++:
                      @
                      "^(((http|ftp)(s?)\:\/\/)|(www\.))(([a-zA-Z0-9\-\.]+(\.[a-zA-Z0-9\-\.]+)+)|localhost)(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_])?([\d\w\.\/\%\+\-\=\&\?\:\\\"\'\,\|\~\;])$"
                      @

                      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