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]QRegExp allow only one white space between strings
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QRegExp allow only one white space between strings

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 11.9k 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.
  • J Offline
    J Offline
    jpincz
    wrote on 25 Jan 2014, 04:48 last edited by
    #1

    Hi, guys maybe you can help with this i really tried but it is very difficult to undertand
    i read from here :https://qt-project.org/doc/qt-4.8/qregexp.html
    if you have a more comprehensive link would be grateful.

    A definition for this example W == any string that does not contain a white space.

    I'm trying to write the following regular expression

    W[space] - - - - - - not allowed (match==false)

    [space]W - - - - - - not allowed (match==false)

    W[space]W - - - - - ALLOWED (match==true)

    W[space][space]W - - - not allowed (match==false)

    W[space]W [space]W - - - ALLOWED (match==true)

    W[space]W [space][space]W - - - not allowed (match==false)

    W[space]W [space]W[space] - - - not allowed (match==false)

    [space]W[space]W [space]W - - - not allowed (match==false)
    so on........
    .............

    SOLUTION:
    QRegExp rx("^\S+(\s\S+)+$");

    still macth this case WORD[SPACE] but works for me , thanks to ChrisW67

    usefull link:
    "RegExp example...":http://qt-project.org/doc/qt-4.8/tools-regexp.html

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Jan 2014, 23:05 last edited by
      #2

      Hi and welcome to devnet,

      Can you provide a concrete string and QRegExp code example ?

      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
      • J Offline
        J Offline
        jpincz
        wrote on 26 Jan 2014, 00:55 last edited by
        #3

        first sorry for my english and thanks for you reply..
        i read this documentation:

        https://qt-project.org/doc/qt-4.8/qregexp.html

        and it si very hard to undertand still i tried , dont put code as example bcz dont want someone answer base on them

        if u have another link with differents or more examples i will be gratefull

        the more closer i get was with this:

        ("^\s*\w+(\s?$| {2,}\w+)+");

        i found taht code here (javascript)

        http://stackoverflow.com/questions/16756507/regexp-which-allow-only-one-space-in-between-words

        but what i need have to match with strings made of word characters and NONword characters(obv excluiding " ") and can have only one space between them but not a first or last as i detailed in my first post

        example of a match:

        @QString s="lucas" + " " + "tornado" + " " + "n°1995" + " " + "12-12-12" + " " + "erkntowertn";@

        examples of a non match:

        @QString s= "lucas" + " ";
        QString s= " " + "lucas";
        QString s= "lucas" + " " + " " + "tornado";@

        i am gonna use it as this on qline edit:

        @ QRegExp rx("MAGIC");
        ui->lineEditDesc->setValidator(new QRegExpValidator(rx, ui->lineEditDesc));@

        for now i use this

        @void NewProduct::on_lineEditDesc_textEdited(const QString &arg1)
        {
        ui-> lineEditDesc-> setText(arg1.simplified());
        }@

        but i that way the user can not put a space on the end of the word to start another word , he has to write all together and then put the spaces

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on 26 Jan 2014, 08:22 last edited by
          #4

          I would think something like:
          @
          QRegExp rx("^\S+(\s\S+)+$"); // if there must be two or more words
          QRegExp rx("^\S+(\s\S+)*$"); // if only one word will do
          @
          would match lines starting and ending with a "word" and containing only single spaces and words in between.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on 26 Jan 2014, 10:42 last edited by
            #5

            You might find the "RegExp example":http://qt-project.org/doc/qt-4.8/tools-regexp.html quite useful for trial and error with different expressions.

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

            1 Reply Last reply
            0

            1/5

            25 Jan 2014, 04:48

            • Login

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