Results 1 to 14 of 14

Thread: Regular expression help neede

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Regular expression help neede

    I have collection of files, the contents of all those files have the following format

    Qt Code:
    1. -- File name
    2. --
    3. -- listOne (L1)
    4. -- listTwo (L2)
    5. -- listThree (L3)
    6. -- HeaderLine (HE)
    7. -- listFour (L6)
    8. -- listFive (L2)
    9. -- listSix (L9)
    10. -- listSeven (L0)
    11. -- someline (SL)
    12. -- listeight (LL)
    13. --
    14. --
    15. REMAINING CONTENTS OF THE LINE
    16. -----------------------------------------------------------------------
    17. some more contents
    18. ------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

    Here i want to store only L1,L2,L3 etc in a list, except HE,SL and remaining lines of files
    How can i do that?
    Please help me, i went through QREgExp class defination also, and i wrote code but that seems to be very big and inserts some blank strings into stored list


    Qt Code:
    1. while(!f.atEnd() && (!line.contains("------------------------------------------")))
    2. {
    3.  
    4. if(!line.contains("-- "))
    5. {
    6. flag=1;
    7. QRegExp rx("[\(]([a-z]|[0-9]|[_]|[A-Z])+[\)]");
    8. rx.indexIn(line);
    9. QRegExp rx1("([a-z]|[0-9]|[_]|[A-Z])+");
    10. rx1.indexIn(rx.cap(0));
    11. captured.append(rx1.cap(0));
    12. line=f.readLine();
    13. }
    14. else if(flag==1)
    15. {
    16. flag++;
    17. captured.pop_back();
    18. QRegExp rx("[\(]([a-z]|[0-9]|[_]|[A-Z])+[\)]");
    19. rx.indexIn(line);
    20. QRegExp rx1("([a-z]|[0-9]|[_]|[A-Z])+");
    21. rx1.indexIn(rx.cap(0));
    22. captured.append(rx1.cap(0));
    23. line=f.readLine();
    24. }
    25.  
    26. else if(flag>0)
    27. { flag++;
    28. QRegExp rx("[\(]([a-z]|[0-9]|[_]|[A-Z])+[\)]");
    29. rx.indexIn(line);
    30. QRegExp rx1("([a-z]|[0-9]|[_]|[A-Z])+");
    31. rx1.indexIn(rx.cap(0));
    32.  
    33.  
    34. captured.append(rx1.cap(0));
    35. line=f.readLine();
    36. }
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 


    Please help me sort this problem
    Last edited by aurora; 13th January 2012 at 11:45.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Regular expression help neede

    Qt Code:
    1. QRegExp rx("[\(]([a-z]|[0-9]|[_]|[A-Z])+[\)]");
    To copy to clipboard, switch view to plain text mode 
    can be written as
    Qt Code:
    1. QRegExp rx("\(([a-z0-9_A-Z])\)");
    To copy to clipboard, switch view to plain text mode 
    which is a littel more readable. Then use QRegExp::capturedTexts() to get the content inside the brackets.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    Sorry Lykurg i posted wrongly...please look at the file format once...
    I dont want to capture "HE" and "LS" like lines which has some sub lines....

  4. #4
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    For example....my input is file shown above,
    and regular expression must capture
    only L1,L2,L3,L6,L2,L9,L0,LL

    it should not capture the line which has subline, thats all…

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

    Default Re: Regular expression help neede

    does everything you want to save start "(L" ?
    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.

  6. #6
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    Quote Originally Posted by amleto View Post
    does everything you want to save start "(L" ?
    No amleto....nothing like that...
    > All texts inside round bracket, which is present at the end of all line.
    > And regular expression should not capture line which has sub line..
    eg:

    Qt Code:
    1. -- its main line
    2. -- its sub line
    3. -- its another subline
    To copy to clipboard, switch view to plain text mode 
    > Consider all the lines which starts with "-- "
    Last edited by aurora; 16th January 2012 at 04:01.

  7. #7
    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: Regular expression help neede

    In your example all the lines you wish to capture start with "-- " and end with "(L.)" where "." is a wildcard, and the unwanted grouping lines do not. Amleto is asking if that is a rule (or something like a rule) you can use. If amleto's observation is correct then this:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7. QFile in("test.txt");
    8. if (in.open(QIODevice::ReadOnly)) {
    9. QTextStream s(&in);
    10.  
    11. QRegExp re("--\\s.*\\((L.)\\)");
    12. while (!s.atEnd()) {
    13. QString line = s.readLine();
    14. if (re.exactMatch(line))
    15. qDebug() << re.cap(1);
    16. }
    17. }
    18. app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    trivially gives:
    Qt Code:
    1. "L1"
    2. "L2"
    3. "L3"
    4. "L6"
    5. "L2"
    6. "L9"
    7. "L0"
    8. "LL"
    To copy to clipboard, switch view to plain text mode 

    If not, you need to infer hierarchy from changes in the amount of whitespace between "-- " and the text of lines ending "\\(.+\\)". Something like:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QFile in("test.txt");
    9. if (in.open(QIODevice::ReadOnly)) {
    10. QTextStream s(&in);
    11.  
    12. int lastDepth = 0;
    13. QString lastValue;
    14. QStringList result;
    15.  
    16. QRegExp re("--(\\s+).*\\((.+)\\)");
    17. while (!s.atEnd()) {
    18. QString line = s.readLine();
    19.  
    20. if (re.exactMatch(line)) {
    21. int newDepth = re.cap(1).length();
    22. if (lastDepth > 0 && newDepth <= lastDepth)
    23. result << lastValue;
    24. lastDepth = newDepth;
    25. lastValue = re.cap(2);
    26. }
    27. else if (line.startsWith("---")) {
    28. if (lastDepth > 0)
    29. result << lastValue;
    30. break;
    31. }
    32. }
    33.  
    34. qDebug() << result;
    35. }
    36.  
    37. app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. ("L1", "L2", "L3", "L6", "L2", "L9", "L0", "LL")
    To copy to clipboard, switch view to plain text mode 
    for your example. This is dependent on reliable indenting which, if humans are involved, is unlikely to be the case.

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

    aurora (19th January 2012)

  9. #8
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    ya chis....
    ur second option is the one i wanted...but i think i need to make changes in regular expression, its not capturing anything
    Would u mind to tell what changes shall i do for that? Please dont mind that regular expression is seems to be bit complex and thats not capturing anything....
    I'm getting confuse to make changes to that....
    Last edited by aurora; 16th January 2012 at 09:37.

  10. #9
    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: Regular expression help neede

    The regular expression and code I gave you does capture the values inside () at the end of suitable lines in your example data. By "end" I mean really the end, no trailing whitespaces for example.

    What exactly are you feeding the routine as input?

  11. #10
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    Quote Originally Posted by ChrisW67 View Post
    The regular expression and code I gave you does capture the values inside () at the end of suitable lines in your example data. By "end" I mean really the end, no trailing whitespaces for example.

    What exactly are you feeding the routine as input?



    i gave a file which is having following contents
    Qt Code:
    1. -- Filename: abcd.txt
    2. -- Player names
    3. -- Geraint Jones (GJa)
    4. -- James Anderson (JA)
    5. -- England (Eng)
    6. -- Captain (cap)
    7. -- KevinPietersen (KPa)
    8. -- Andrew Strauss (AS)
    9. -- Paul Collingwood (PC)
    10. -- Flintoff (AF)
    11. -- Australia (Au)
    12. -- Manager (Man)
    13. -- Ponting (RP)
    14. -- Adam Gilchrist (AG)
    15. -- Brad Hogg (BH)
    16. --
    17. --
    To copy to clipboard, switch view to plain text mode 


    Here it should capture only the Player names....and should not capture country name and designation
    This is the input i have given but that regular expression didnt capture anything...
    Last edited by aurora; 19th January 2012 at 05:38.

  12. #11
    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: Regular expression help neede

    This is the input i have given but that regular expression didnt capture anything...
    You sure? Really sure?

    Using the code exactly as I posted above, with a test.txt containing exactly what you just posted I get:
    Qt Code:
    1. ("GJa", "JA", "KPa", "AS", "PC", "AF", "RP", "AG")
    To copy to clipboard, switch view to plain text mode 

    The country entries "Eng" and "Au" are missing because your stated requirement was that we "should not capture line which has sub line". The same reasoning excludes the "cap" and "Man" entries. Hoggy (BH) is missing because your example text does not meet the format you posted at the start of this thread, i.e. a line of hyphens terminating the header triggered the output of the last pending entry.

    Have you tried to understand how the code functions?

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

    aurora (19th January 2012)

  14. #12
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    Thanks a lot cris, some minor correction needed in my main program, now it works as i expected.
    Thank u so much for looking at my issue with that much Patient...

  15. #13
    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: Regular expression help neede

    A variation that captures BH as well:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QFile in("test.txt");
    9. if (in.open(QIODevice::ReadOnly)) {
    10. QTextStream s(&in);
    11.  
    12. int lastDepth = 0;
    13. QStringList result;
    14.  
    15. QRegExp re("--(\\s+).*\\((.+)\\)");
    16. while (!s.atEnd()) {
    17. QString line = s.readLine();
    18.  
    19. if (re.exactMatch(line)) {
    20. int newDepth = re.cap(1).length();
    21. if (newDepth > lastDepth && result.size() > 0)
    22. result.removeLast();
    23. lastDepth = newDepth;
    24. result << re.cap(2);
    25. }
    26. else if (line.startsWith("---"))
    27. break;
    28. }
    29.  
    30. qDebug() << result;
    31. }
    32.  
    33. app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

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

    aurora (19th January 2012)

  17. #14
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Regular expression help neede

    Ok...Thank u once again Chris....

Similar Threads

  1. Regular expression
    By QFreeCamellia in forum Newbie
    Replies: 8
    Last Post: 30th December 2011, 22:34
  2. Regular expression help!
    By ConkerX in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2011, 15:47
  3. Help with regular expression
    By Gourmet in forum General Programming
    Replies: 19
    Last Post: 11th August 2011, 15:04
  4. Regular Expression Problem
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 09:41
  5. set a regular expression on QTextEdit
    By mattia in forum Newbie
    Replies: 3
    Last Post: 27th March 2008, 10:16

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.