Results 1 to 3 of 3

Thread: using Regular Expression for simple searching

  1. #1
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default using Regular Expression for simple searching

    I was recently given an assignment for a C++ class im currently taking to search the given string

    BRSLPMSLPRESLPSLPSLPABSLPENDRPCFPSLPABXDSLPRMNPLSL RSLPBESLPASLPRTWPSLPXBTSLPSLPM

    using a character array approach, for occurrences of the substring SLP and output the number of times SLP occurs. Error conditions for searching the string are:
    1. SLP cannot be preceded by E. ex.) ESLP
    2. SLP cannot be followed by A. ex.)SLPA
    3. SLP cannot be preceded by B followed by any character. ex.) BASLP , BBSLP ……., BZSLP

    But using a char array approach is too simple and boring, for me the solution was
    Qt Code:
    1. int count =0;
    2. QString data = "BRSLPMSLPRESLPSLPSLPABSLPENDRPCFPSLPABXDSLPRMNPLSLRSLPBESLPASLPRTWPSLPXBTSLPSLPM";
    3. for( int i =0; i < data.length(); ++i){
    4.  
    5. if(data[i] == QChar('S') && data[i+1] == QChar('L') && data[i+2] == QChar('P')){
    6. if(data[i-2] != QChar('B') && data[i-1] != QChar('E') &&data[i+3] != QChar('A'))
    7. ++count;
    8. }
    9. }
    10. // count = 8
    To copy to clipboard, switch view to plain text mode 

    so i decided to see if i could accomplish the same thing using regular expressions, which i just recently started learning on my own time.


    Qt Code:
    1. #include <iostream>
    2. #include <QRegExp>
    3. #include <QString>
    4. int
    5. int main(){
    6. QString data = "BRSLPMSLPRESLPSLPSLPABSLPENDRPCFPSLPABXDSLPRMNPLSLRSLPBESLPASLPRTWPSLPXBTSLPSLPM"
    7. QRegExp regxTotal("SLP+"); // match string "SLP" - output is 14
    8. QRegExp aCount("SLPA+"); // match string "SLPA" - output is 3
    9. QRegExp eCount("ESLP+"); // match string "ESLP" output is 2
    10. QRegExp bCount("B.SLP+"); // match string beginning with B followed by any character followed by SLP - output is 3
    11. QRegExp woErrors("([^E]SLP[^A])"); // match SLP that do not meet error conditions
    12. /*
    13.  
    14. Attempted variations
    15.   ((?!E)SLP+|(?!B.)SLP+|SLP(?!A)+) output is 14
    16.   ((?!ESLP)+|(?!B.SLP)+|(?!ASLP)+) no output, causes infinite loop
    17.   ((?!ESLP)SLP+|(?!B.SLP)SLP+|(?!ASLP)SLP+) output is 14
    18. */
    19.  
    20.  
    21. std:: cout << "" << courtOccurances(regxTotal, data) << std::endl; // output is 14
    22. std:: cout << "" << courtOccurances(aCount, data) << std::endl; // output is 3
    23. std:: cout << "" << courtOccurances(eCount, data) << std::endl; // output is 2
    24. std:: cout << "" << courtOccurances(bCount, data) << std::endl; // output is 3
    25. std:: cout << "" << courtOccurances(woErrors, data,true) << std::endl; // output should be 8
    26.  
    27.  
    28. return 0;
    29.  
    30. }//end main
    31.  
    32.  
    33.  
    34. int countOccurances(QRegExp & regx, QString& data, bool debug = false){
    35. int count = 0; // occurances
    36. int pos = 0; // current pos inside of 'data'
    37. while ((pos = regx.indexIn(data, pos)) != -1) {
    38. ++count;
    39. pos += regx.matchedLength();
    40.  
    41. if(debug){ // print captured texts to console for debug
    42. QStringList tmp = regx.capturedTexts();
    43. while(! tmp.isEmpty()){
    44. std::cout << tmp.first().toStdString() << std::endl;
    45. tmp.removeFirst();
    46. }// end while
    47. }//end if
    48. }// end while
    49. if(debug)// print debug output seperator
    50. std::cout << "+++++++++++++++++"<< data.toStdString() <<std::endl;
    51.  
    52.  
    53. return count;
    54. }// end fucntion
    To copy to clipboard, switch view to plain text mode 

    Output of captured texts
    Qt Code:
    1. ((?!E)SLP+|(?!B.)SLP+|SLP(?!A)+)
    2. SLP
    3. SLP
    4. SLP
    5. SLP
    6. SLP
    7. SLP
    8. SLP
    9. SLP
    10. SLP
    11. SLP
    12. SLP
    13. SLP
    14. SLP
    15. SLP
    16. SLP
    17. SLP
    18. SLP
    19. SLP
    20. SLP
    21. SLP
    22. SLP
    23. SLP
    24. SLP
    25. SLP
    26. SLP
    27. SLP
    28. SLP
    29. SLP
    30.  
    31. ((?!ESLP)+|(?!B.SLP)+|(?!SLPA)+)
    32. expression unmatched- somehow causes infinite loop
    33.  
    34. ((?!ESLP)SLP+|(?!B.SLP)SLP+|(?!ASLP)SLP+) count is 14
    35.  
    36. SLP
    37. SLP
    38. SLP
    39. SLP
    40. SLP
    41. SLP
    42. SLP
    43. SLP
    44. SLP
    45. SLP
    46. SLP
    47. SLP
    48. SLP
    49. SLP
    50. SLP
    51. SLP
    52. SLP
    53. SLP
    54. SLP
    55. SLP
    56. SLP
    57. SLP
    58. SLP
    59. SLP
    60. SLP
    61. SLP
    62. SLP
    63. SLP
    To copy to clipboard, switch view to plain text mode 


    any tips for how I can get QRegExp to meet my error conditions? if any more info is needed plz ask. thanks for the help.
    artificial intelligence is no match for natural stupidity

  2. #2
    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: using Regular Expression for simple searching

    Your search approach is a bit naive. If your array has n characters, you have to do n iterations over the array. That's too much, it is possible to do the search more efficiently (without using regular expressions).
    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.


  3. #3
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: using Regular Expression for simple searching

    it is possible to do the search more efficiently (without using regular expressions).
    I am aware that there are more efficient ways to approach searching a string( most of which i don't know how to implement ) , i just wanted to try i using regular expressions to see if i could get the same results. the program itself is for a class assignment so i am not really concerned about efficiency, just getting the correct result.
    artificial intelligence is no match for natural stupidity

Similar Threads

  1. Regular expression help!
    By ConkerX in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2011, 15:47
  2. Help with regular expression
    By Gourmet in forum General Programming
    Replies: 19
    Last Post: 11th August 2011, 15:04
  3. set a regular expression on QTextEdit
    By mattia in forum Newbie
    Replies: 3
    Last Post: 27th March 2008, 10:16
  4. Regular expression in QLineEdit?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 1st October 2007, 10:58
  5. Find with Regular Expression?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 14:44

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.