[Solved] regex not working in qt



  • 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


  • Moderators

    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


  • Lifetime Qt Champion

    Hi,

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



  • 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?



  • 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?


  • Moderators

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



  • 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
    ....
    }
    @


  • Moderators

    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.


  • Lifetime Qt Champion

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



  • [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\.\/\%\+\-\=\&\?\:\\\"\'\,\|\~\;])$"
    @


Log in to reply
 

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