Results 1 to 4 of 4

Thread: QRegExp Confusion

  1. #1
    Join Date
    Apr 2009
    Posts
    12

    Default QRegExp Confusion

    I'm helping a friend of mine port a small application he's written from C# .NET to C++ QT and we've come across a little snag with the regular expressions.

    In C# .NET we have a piece of code which looks similar to this:

    Qt Code:
    1. private bool ValidURL(string url)
    2. {
    3. RegEx validURLRegex = new Regex("^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/?.*)$");
    4. if (validURLRegex.IsMatch(url))
    5. return true;
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I'm trying to convert this to use QRegExp (which I'm new to) and the code I come up with is this:

    Qt Code:
    1. void SomeClass::ValidURL(QString url)
    2. {
    3. QRegExp validURLRegex("^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/?.*)$");
    4. if (validURLRegex.exactMatch(url))
    5. return true;
    6. return false;
    7. }
    To copy to clipboard, switch view to plain text mode 

    This doesn't seem to work, however. I've tried various things, such as calling setMinimal(true) (not sure whether .NET uses greedy matching or not) and I've also tried setting the pattern syntax to all the available options, to no avail.

    Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp Confusion

    Hi, may you could use QUrl::isValid() and then I am not sure if every "." should have the meaning "any character. If you mean only a point use "\\."

    EDIT: [-.]{1} should be [\\-\\.]{1} etc.

  3. #3
    Join Date
    Apr 2009
    Posts
    12

    Default Re: QRegExp Confusion

    I did spot QUrl::IsValid() but that seemed to return true no matter what I gave it. Even simple strings such as "notavalidurl" would return true.

  4. #4

    Default Re: QRegExp Confusion

    Try coupling your QRegExp with a QRegExpValidator. Use the QRegExpValidator function validate(...) to check... Something along the lines of this should do the trick:

    Qt Code:
    1. void SomeClass::ValidURL(QString url)
    2. QRegExp validURLRegex("^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/?.*)$");
    3. QRegExpValidator validator(validURLRegex);
    4. int index = 0;
    5. if (validator.validate(url, index) == QValidator::Acceptable)) {
    6. return true;
    7. } else {
    8. return false;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    As a bonus, you could use your QRegExp/QRegExpValidator with an input widget (think QLineEdit) so that only valid values can be entered.... (Not sure how this fits into the greater scheme, so maybe this isn't appropriate for you..)

Similar Threads

  1. Two Word QRegExp Help
    By mabeeh in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2008, 14:29
  2. QRegExp Help
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2007, 00:13
  3. QRegExp progblem
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2006, 12:12
  4. need help for my QRegExp
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2006, 16:29
  5. Trouble parsing using simple QRegExp
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2006, 00:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.