Results 1 to 11 of 11

Thread: Regular expression help!

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Regular expression help!

    Hi,
    I'm need a help to make a regular expression.
    I need a regular expression that validate a string with the following rules:

    1- The first character allowed are a-z and A-Z.
    2- Is allowed to put after the the first character a-z, A-Z, 0-9, . and -.
    3- It not allowed sequence of period(.). (e.g. "qt..framework" its not valid)
    5- The string can be infinity.
    6- The end of string can't contain . or -.

    I tryed to do my own regexp and the result was "(([a-zA-Z][-a-zA-Z0-9]*(\\.[-a-zA-Z0-9]+)*)[^-.]+$)".
    My huge problem its to validate the end of string, checking if there is some - or ..
    When i put "[^-.]+$" i have two problens one is that i get Intermediate result from
    QRegExpValidator::validate() and second is when i put only a character to be
    validate i got Intermediate result too.

    Can someone help me?
    Thanks
    Last edited by ConkerX; 30th August 2011 at 19:46.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Regular expression help!

    When I get you right, your string can only consist of characters a-z, A-Z, 0-9 and "." and "-". According to 6 above, your string then ends in one of the characters a-z, A-Z or 0-9. So you could replace "[^-.]+$" by "[a-zA-Z0-9]$". Maybe that helps.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Regular expression help!

    Get rid of all those parentheses. You don't need them unless you're trying to capture sub-matches, and the way they're arranged they're not going to do that in any meaningful way.

  4. #4
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regular expression help!

    Quote Originally Posted by ars View Post
    When I get you right, your string can only consist of characters a-z, A-Z, 0-9 and "." and "-". According to 6 above, your string then ends in one of the characters a-z, A-Z or 0-9. So you could replace "[^-.]+$" by "[a-zA-Z0-9]$". Maybe that helps.
    Thanks ars,
    I already tryed this and still give me the "Intermediate" result when try for example "test-" or "t".

    Here is how i substitute with your sugestion:
    (([a-zA-Z][-a-zA-Z0-9]*(\\.[-a-zA-Z0-9]+)*)[a-zA-Z0-9]$)

    Sry i posted the wrong regexp that i did.

    This is the right one:
    (([a-zA-Z][-a-zA-Z0-9]*(\\.[-a-zA-Z0-9]+)*)[^-.]+$)


    Quote Originally Posted by SixDegrees View Post
    Get rid of all those parentheses. You don't need them unless you're trying to capture sub-matches, and the way they're arranged they're not going to do that in any meaningful way.
    Thank for the help SixDegrees,

    Sry i posted the wrong regexp that i did.

    This is the right one:
    (([a-zA-Z][-a-zA-Z0-9]*(\\.[-a-zA-Z0-9]+)*)[^-.]+$)


    I can't take off the parentheses if i take off i get wrong results.

  5. #5
    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: Regular expression help!

    You get the Intermediate state because you should get it. Because the input can become valid very likely by the next input. That is how a validator works. And why is that a problem? You can check for !QValidator::Acceptable. Or use QRegExp::exactMatch.

    And to the problem if only one char is typed (if that input should be ok) you have to do something like that:
    Qt Code:
    1. (your pattern | ^[0-9a-zA-Z]{1}$)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regular expression help!

    Quote Originally Posted by Lykurg View Post
    You get the Intermediate state because you should get it. Because the input can become valid very likely by the next input. That is how a validator works. And why is that a problem? You can check for !QValidator::Acceptable. Or use QRegExp::exactMatch.

    And to the problem if only one char is typed (if that input should be ok) you have to do something like that:
    Qt Code:
    1. (your pattern | ^[0-9a-zA-Z]{1}$)
    To copy to clipboard, switch view to plain text mode 
    Thank for the help Lykurg,

    I didn't know about this method QRegExp::exactMatch, maybe i will use it.

    Your pattern ^[0-9a-zA-Z]{1}$ dont fit with my rules. The user can type "test--" and this string is not valid.
    With my pattern (([a-zA-Z][-a-zA-Z0-9]*(\\.[-a-zA-Z0-9]+)*)[^-.]+$) i almost get the right result.
    I just have problem with the '.' and - at the end of string and with a single letter string.
    e.g "a" with my pattern i got false with exactMatch and Intermediate with validate() and the "a" alone is a valid string.

    Example of valid and not valid strings:
    2test = not valid
    -test = not valid
    .test = not valid
    test = not valid
    test = valid
    test2 = valid
    testX = valid
    test. = not valid
    test-.-. = not valid
    t = valid
    test..t = not valid
    te.s.t = valid
    t.es-t = valid
    tes--t = valid
    t2est = valid
    te3443st = valid

  7. #7
    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: Regular expression help!

    Oh boy, what I was trying to tell you was:
    Qt Code:
    1. QRegExp rx("^([a-zA-Z]+([-a-zA-Z0-9]|\\.[-a-zA-Z0-9]+)*|[a-zA-Z]{1})$");
    2. QStringList teststrings = QString("2test -test .test test test2 testX test. test-.-. t test..t te.s.t t.es-t tes--t t2est te3443st").split(" ");
    3. for (int i = 0; i < teststrings.count(); ++i)
    4. qWarning() << teststrings.at(i) << rx.exactMatch(teststrings.at(i));
    To copy to clipboard, switch view to plain text mode 
    "2test" false
    "-test" false
    ".test" false
    "test" true
    "test2" true
    "testX" true
    "test." false
    "test-.-." false
    "t" true
    "test..t" false
    "te.s.t" true
    "t.es-t" true
    "tes--t" true
    "t2est" true
    "te3443st" true
    ...or even more easier:
    Qt Code:
    1. QRegExp rx("^[a-zA-Z]+([-a-zA-Z0-9]|\\.[-a-zA-Z0-9]+)*$");
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regular expression help!

    Quote Originally Posted by Lykurg View Post
    Oh boy, what I was trying to tell you was:
    Qt Code:
    1. QRegExp rx("^([a-zA-Z]+([-a-zA-Z0-9]|\\.[-a-zA-Z0-9]+)*|[a-zA-Z]{1})$");
    2. QStringList teststrings = QString("2test -test .test test test2 testX test. test-.-. t test..t te.s.t t.es-t tes--t t2est te3443st").split(" ");
    3. for (int i = 0; i < teststrings.count(); ++i)
    4. qWarning() << teststrings.at(i) << rx.exactMatch(teststrings.at(i));
    To copy to clipboard, switch view to plain text mode 
    ...or even more easier:
    Qt Code:
    1. QRegExp rx("^[a-zA-Z]+([-a-zA-Z0-9]|\\.[-a-zA-Z0-9]+)*$");
    To copy to clipboard, switch view to plain text mode 
    Thanks Lykurg for your help!

    I got surprise with your result but i start to try with more words and i saw a error with some strings i forgot to add to the list.

    Add those strings to your list:
    test-- = Not valid (With your pattern is valid)
    test- = Not valid (With your pattern is valid)
    test.. = Not valid (With your pattern is Ok i just put to dont forget to test )
    Qt Code:
    1. QStringList teststrings = QString("2test -test .test test test2 testX test. test-.-. t test..t te.s.t t.es-t tes--t t2est te3443st test-- test- test..").split(" ");
    To copy to clipboard, switch view to plain text mode 

    Thanks for your dedication in helping me!

  9. #9
    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: Regular expression help!

    Well, that is your part to add the exceptions for "-". Also should "aa.-bb" be valid?

  10. #10
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regular expression help!

    That is my doubt how to don't allow a - or . at the end of string, i tryed with [^-.]+$ at the end of my pattern but the string "t" get invalid.

    My pattern:
    Qt Code:
    1. "(([a-zA-Z][-a-zA-Z0-9]*(\\.[-a-zA-Z0-9]+)*)[^-.]+$)"
    To copy to clipboard, switch view to plain text mode 

    The string "aa.-bb" its not valid too.

  11. #11
    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: Regular expression help!

    In your approach a single t is "captured" with the first [a-zA-Z] and then the pattern requires an other one by [^-.]+. That is the problem. So why do you don't try to understand my last solution and simple alter it by applying the same rules for . also for -? As a hint:

    Characters to be deleted: --
    Characters to be added: )(|-
    May by you also need: 1}{ but I don't think so.

    Go ahead!

Similar Threads

  1. Help with regular expression
    By Gourmet in forum General Programming
    Replies: 19
    Last Post: 11th August 2011, 15:04
  2. Regular Expression Problem
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 09:41
  3. set a regular expression on QTextEdit
    By mattia in forum Newbie
    Replies: 3
    Last Post: 27th March 2008, 10:16
  4. Regular expression in QLineEdit?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 1st October 2007, 10:58
  5. Find with Regular Expression?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 14:44

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.