Results 1 to 6 of 6

Thread: How to split string using QRegex?

  1. #1
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to split string using QRegex?

    Hello,

    I want to split this: {($..Properties[*])-($..Layer_Index)}
    Like: ("$..Properties[*]", "$..Layer_Index")

    I have tried some examples and online regex tester, I'm getting output but it's not working here.

    On this link: https://www.regextester.com/97589
    I have tried with \(.*?\)
    But in code, it's not working.

    I have tried with many expressions but not working

    What I'm missing?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to split string using QRegex?

    What I'm missing?
    Are you properly "escaping" your regex when you pass it as a QString?

    Qt Code:
    1. "\\(.*?\\)"
    To copy to clipboard, switch view to plain text mode 

    If the strings you want to split are all this simple, why not just split on "-" and then remove the "(" and ")" using QString::left() and QString::right()?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to split string using QRegex?

    @d_stranz, I cant use the method you suggest because the sign between the two expressions can be changed to anything.

    FOr reference I have use below code
    Qt Code:
    1. QString query("{($..Properties[*])-($..Layer_Index)}");
    2. QStringList list = query.split(QRegularExpression("\\(.*?\\)"));
    3. qDebug()<<"Output: "<<list;
    4.  
    5. //Output --> Output: ("{", "-", "}")
    To copy to clipboard, switch view to plain text mode 

    And I have checked on many online regex testers like this one https://regexr.com/
    And output is like bold marked: {($..Properties[*])-($..Layer_Index)}

    So why there is so differences in this outputs.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to split string using QRegex?

    The first argument to QString::split() is the delimiter you want to split the string on. You are telling QString to split using parentheses-enclosed strings as the delimiter, and keep the other bits. You get the opening '{', the separating '-', and the closing '}' because these are the only bits outside the delimiters. Try this RE as the delimiter "[{}-]" with QString::SkipEmptyParts.

    If you want to use a regular expression to match the bits of the strings you want then then that is better achieved with QRegularExpression.

  5. #5
    Join Date
    Jun 2015
    Posts
    109
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to split string using QRegex?

    Thanks,

    I have tried with other way, but it has small issue, look the code below,
    Qt Code:
    1. QString query("{($..Properties[*])-($..Layer_Index)}");
    2. QRegularExpression re;
    3. re.setPattern("\\(.*?\\)");
    4. QRegularExpressionMatch match = re.match(query);
    5. qDebug()<<"Match 0: "<<match.captured(0);
    6. qDebug()<<"Match 1: "<<match.captured(1);
    7.  
    8. //Output: Match 0: "($..Properties[*])"
    9. // Match 1: ""
    To copy to clipboard, switch view to plain text mode 

    Here I didnt get the Match 1 as ($..Layer_Index), why ?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to split string using QRegex?

    For one thing, QRegularExpressionMatch::captured() with a '0' argument returns the -entire- capture, not the first one. captured( 1 ) should be returning your first string, not the second. QRegularExpressionMatch::capturedTexts() will return all of them as a QStringList.

    And try using QRegularExpression::globalMatch() instead of match().
    Last edited by d_stranz; 14th April 2020 at 16:57.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. how to split a big string?
    By aurora in forum Newbie
    Replies: 2
    Last Post: 21st February 2012, 06:10
  2. Split string
    By mero in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2011, 19:17
  3. split camel case string
    By GrahamLabdon in forum Newbie
    Replies: 5
    Last Post: 1st February 2011, 16:37
  4. How to split a string line
    By grsandeep85 in forum Newbie
    Replies: 5
    Last Post: 29th July 2009, 10:42
  5. How to split a string line
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 10:28

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.