Results 1 to 8 of 8

Thread: separate numbers of texts

  1. #1
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default separate numbers of texts

    Regular expression that will isolate numbers and text.

    Qt Code:
    1. QStringList linesInt;
    2. QRegExp rx("(\\d+)");
    3. QString text = "5001 1001 5002 1002.5 Observation reason: 10 river and pond."
    4.  
    5. int pos = 0;
    6. while ((pos = rx.indexIn(text,pos)) != -1){
    7. linesInt << rx.cap(1);
    8. pos += rx.matchedLength();
    9. }
    10. //linesInt = (5001, 1001, 5002, 1002, 5, 10) --- I want linesInt = (5001, 1001, 5002, 1002.5)
    11. //or linesInt = (5001, 1001, 5002, 1002.5, Observation reason: 10 river and pond.)
    To copy to clipboard, switch view to plain text mode 

    The code below give me the result I expect. But I would have to test if the first 4 are numbers.

    Qt Code:
    1. QStringList linesInt;
    2. QRegExp rx("\t");
    3. QString text = "5001\t1001\t5002\t1002.5\tObservation reason: 10 river and pond."
    4. linesInt = text.split(rx,QString::SkipEmptyParts);
    5. //linesInt = (5001, 1001, 5002, 1002.5, Observation reason: 10 river and pond.)
    To copy to clipboard, switch view to plain text mode 

    Someone would indicate a QRegExp?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: separate numbers of texts

    A simple improvement of your first regexp:
    Qt Code:
    1. QRegExp rx("(\\d+\\.?\\d*)");
    To copy to clipboard, switch view to plain text mode 
    but it will catch the last "10" as well (and numbers like "1000." too).

  3. #3
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: separate numbers of texts

    Quote Originally Posted by stampede View Post
    A simple improvement of your first regexp:
    Qt Code:
    1. QRegExp rx("(\\d+\\.?\\d*)");
    To copy to clipboard, switch view to plain text mode 
    but it will catch the last "10" as well (and numbers like "1000." too).
    Thanks stampede!

    Really, he will catch the last "10" and numbers like "1000.". I'm trying but have not found the solution.
    The problem will also be isolate the text of string.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: separate numbers of texts

    I think catching strings like "1000." is actually a good thing. For example, in C, 100. is as good as 100.0
    You can fix this by replacing the '*' with '+' if you dont like it.

  5. #5
    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: separate numbers of texts

    Or
    Qt Code:
    1. QRegexp rx("(\\d+(\\.\\d+)?)");
    To copy to clipboard, switch view to plain text mode 
    will capture the dot in a number if it is followed by other digits but not otherwise. It really depends on what you need.
    You hint that input string is not indicative of your entire possible inputs (variable length of number list). You could do something like:
    Qt Code:
    1. QRegexp rx("^((?:(?:\\d+(?:\\.\\d+)?)\\s+)*)(.*)");
    To copy to clipboard, switch view to plain text mode 
    and then split group 1 on white space to get only the leading numbers. Group 2 contains the trailing remainder.

  6. #6
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: separate numbers of texts

    Quote Originally Posted by ChrisW67 View Post
    Or
    Qt Code:
    1. QRegexp rx("(\\d+(\\.\\d+)?)");
    To copy to clipboard, switch view to plain text mode 
    will capture the dot in a number if it is followed by other digits but not otherwise. It really depends on what you need.
    You hint that input string is not indicative of your entire possible inputs (variable length of number list). You could do something like:
    Qt Code:
    1. QRegexp rx("^((?:(?:\\d+(?:\\.\\d+)?)\\s+)*)(.*)");
    To copy to clipboard, switch view to plain text mode 
    and then split group 1 on white space to get only the leading numbers. Group 2 contains the trailing remainder.
    Thanks ChrisW67,

    I used the code below and it worked well. But the problem continues to capture "1000." not excluding the dot and not considered as a real value.
    Qt Code:
    1. QStringList lines, linesInt;
    2. QString linesStr;
    3. QRegExp rxIn("^((?:(?:\\d+(?:\\.\\d+)?)\\s+)*)(.*)");
    4. int pos = 0;
    5. while ((pos = rxIn.indexIn(lines[i],pos)) != -1){
    6. linesInt << rxIn.cap(1).split("\t",QString::SkipEmptyParts);
    7. linesStr = rxIn.cap(2);
    8. pos += rxIn.matchedLength();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jaca; 13th October 2011 at 14:03.

  7. #7
    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: separate numbers of texts

    Unless there is a good reason not to, I would suggest to parse the text manually instead of using regular expressions. It seems you are focused on one class of characters (digits+dot) so using a regular expression will not be much faster (if at all) than parsing the string manually and you will save a lot of time trying to get the expression right. You can even use some kind of parser generator if you want.
    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.


  8. #8
    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: separate numbers of texts

    Quote Originally Posted by jaca View Post
    Thanks ChrisW67,
    I used the code below and it worked well.
    You have misused the anchored regular expression. You should match it against each line once and then split the first capture group. There is no while() loop required.
    Qt Code:
    1. lines
    2. << "5001 1001 5002 1002.5 Observation reason: 10 river and pond."
    3. << "500. 1001 5002 1002.7 Observation reason: 20 lake or dam"
    4. << "5001 100. 5002 1002.7 Observation reason: 20 lake or dam"
    5. << "30.1 30.1 30 Observation reason: 10 river and pond.";
    6.  
    7. QRegExp rx("^((?:(?:\\d+(?:\\.\\d+)?)\\s+)*)(.*)");
    8. // QRegExp rx("^((?:(?:\\d+\\.?\\d*)\\s+)*)(.*)");
    9. foreach(QString line, lines) {
    10. if (rx.indexIn(line) != -1) {
    11. qDebug() << "Numbers:" << rx.cap(1).split(' ', QString::SkipEmptyParts) << "Remainder" << rx.cap(2);
    12. }
    13. else
    14. qDebug() << "No match:" << line;
    15. }
    To copy to clipboard, switch view to plain text mode 
    The expression treats "500." as not-a-number and stops capturing at the first non-number. The commented expression includes these as numbers but you get the dot which is fine in most circumstances.

    I agree, by the way, with wysota that the problem is probably easier to handle manually.

  9. The following user says thank you to ChrisW67 for this useful post:

    jaca (14th October 2011)

Similar Threads

  1. Replies: 5
    Last Post: 2nd November 2012, 03:38
  2. Replies: 1
    Last Post: 30th August 2011, 02:01
  3. Select texts using the QRubberband
    By charlesprime in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 06:14
  4. How to hange color for different texts in plaintextedit?
    By sudhansu in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2010, 13:36
  5. QSpinBox how to separate the numbers
    By aekilic in forum Qt Programming
    Replies: 3
    Last Post: 3rd September 2007, 16:37

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.