Results 1 to 7 of 7

Thread: Problem with regular expression not working

  1. #1

    Default Problem with regular expression not working

    Hi all

    I'm trying to find a working regular expression for make this operation:

    I have a text formatted as below:

    {text1}{text2}{text3}{text4}.....{textN}

    I need a regular expression for parse this text and "extract" a list of strings inside the {}. So the result should be a list like:

    text1
    text2
    text3
    text4
    ...
    textN

    I tried using the following code:

    Qt Code:
    1. QRegExp RegExp("\\{(.*?)\\}");
    2. int Pos;
    3.  
    4. Pos = RegExp.indexIn(MyText);
    5. List = RegExp.capturedTexts();
    To copy to clipboard, switch view to plain text mode 

    but doesn't work, capturedTexts() return me a list with only two empty lines. I tested the regular expression \\{(.*?)\\} in a simulator and it work as expected but it seem QT doesn't accept it.

    Someone can suggest me the right regular expression?

    Thank you

  2. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with regular expression not working

    The old QRegExp class does not support the non-greedy operator .*?, you need to call QRegExp::setMinimal(true) for that. Also, you are supposed to iterate over the captured texts:
    Qt Code:
    1. QRegExp r("\\{(.*)\\}");
    2. r.setMinimal(true);
    3.  
    4. int pos = 0;
    5.  
    6. while ((pos = r.indexIn(myText, pos)) != -1)
    7. {
    8. qDebug() << r.cap(1);
    9. pos += r.matchedLength();
    10. }
    To copy to clipboard, switch view to plain text mode 
    If you are working with Qt 5, you can use the new QRegularExpression:
    Qt Code:
    1. QRegularExpression r("\\{(.*?)\\}");
    2. QRegularExpressionMatchIterator i = r.globalMatch(myText);
    3.  
    4. while (i.hasNext())
    5. {
    6. QRegularExpressionMatch match = i.next();
    7. qDebug() << match.captured(1);
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with regular expression not working

    This should work too:

    Qt Code:
    1. QRegExp rx("\\{([\\}]+)\\}");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with regular expression not working

    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5

    Default Re: Problem with regular expression not working

    Quote Originally Posted by mattc View Post
    The old QRegExp class does not support the non-greedy operator .*?, you need to call QRegExp::setMinimal(true) for that. Also, you are supposed to iterate over the captured texts:
    Qt Code:
    1. QRegExp r("\\{(.*)\\}");
    2. r.setMinimal(true);
    3.  
    4. int pos = 0;
    5.  
    6. while ((pos = r.indexIn(myText, pos)) != -1)
    7. {
    8. qDebug() << r.cap(1);
    9. pos += r.matchedLength();
    10. }
    To copy to clipboard, switch view to plain text mode 
    This solution work very well, thank you so much for the help.

    I need to read the QRegExp documentation again since I didn't note the "unsupported" operator. I suppose is my fault since I'm not expert in regular expression...

    Anyway thank you again to all

  6. #6

    Default Re: Problem with regular expression not working

    Sorry, I need to ask help again for a regular expression. Unfortunately regular expression still give me the headache.

    I have an example text like the following string:

    achuweshurdddjhukk

    I want to replace using the call of QString all the occurence of the substring "hu" that doesn't start with 'c' or 's'. The regular expression "[^cs]hu" is able to make this job but my problem is that this expression extract the "complete" string, in this example "jhu". On the contrary I need only to have "hu" without the "discrimination" letter. This because using the replace function:

    QString Text("achuweshurdddjhukk");

    Text.replace(QRegExp("[^cs]hu"), "XX");

    I'll have as result:

    "achuweshurdddXXkk"

    and not

    "achuweshurdddjXXkk"

    as I need. In the "wrong" case also the 'j' letter near the matched "hu" is replaced.

    Someone can help me fixing thie regular expression?

    Thank you for the patience

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with regular expression not working

    looks like you will have to work your way through the string and replace matches as you find them. What you want is a 'look behind' regex, which I don't see in Qt 4.x
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Regular expression
    By aguleo in forum Newbie
    Replies: 0
    Last Post: 25th January 2013, 16:00
  2. Regular expression
    By QFreeCamellia in forum Newbie
    Replies: 8
    Last Post: 30th December 2011, 23:34
  3. Regular expression help!
    By ConkerX in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2011, 16:47
  4. Help with regular expression
    By Gourmet in forum General Programming
    Replies: 19
    Last Post: 11th August 2011, 16:04
  5. Regular Expression Problem
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 10:41

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.