Important: Please read the Qt Code of Conduct -

[SOLVED]QRegExp allow only one white space between strings

  • Hi, guys maybe you can help with this i really tried but it is very difficult to undertand
    i read from here :
    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........

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

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

    usefull link:
    "RegExp example...":

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you provide a concrete string and QRegExp code example ?

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

    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)

    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

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

  • You might find the "RegExp example": quite useful for trial and error with different expressions.

Log in to reply