Qt 正则表达式 用QRegularExpression代替QRegExp

前端之家收集整理的这篇文章主要介绍了Qt 正则表达式 用QRegularExpression代替QRegExp前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

QRegularExpression 是Qt 5.0才引进的,相对于QRegExp,QRegularExpression class修复了很多bug,提高了效率,提供了对Perl的RegEx几乎全面兼容的搜索引擎。简单说,QRegExp年久失修,bug较多,使用时建议使用QRegularExpression。
注意:若在正则表达式中需要用到”\”,需要在它前面补一个转义字符”\”,因为”\”在字符串中是一个转义字符会将后面的字符换算成ASCII码进行转义。所以如果要将”\”传递到正则当中就需要多添加一个”\”做转义。

  1. #include <QCoreApplication>
  2. #include <QDebug>
  3. #include <QRegExp>
  4. #include <QRegularExpression>
  5. #include <QRegularExpressionMatch>
  6.  
  7. int main(int argc,char *argv[])
  8. {
  9. QString pattern1 = ".*=\\d*";
  10. QString pattern2 = "[a-z]*=\\d*";
  11. QString pattern3 = "\\w+";
  12. QString pattern4 = "(.+)=(.+)";
  13.  
  14. QString test1("a=123");
  15. QString test2("-\r\na=123abc");
  16. QString test3("-_%\r\na=1abc-_%\r\na=12abc-_%\r\na=123abc-_%\r\na=1234abc");
  17. QString test4("hello world,hello qt");
  18.  
  19. //test="a=123",pattern=".*=\\d*"
  20. QRegExp regExp(pattern1);
  21. qDebug()<<regExp.exactMatch(test1);//true
  22.  
  23. //test="a=-\r\na=123abc",pattern="[a-z]*=\\d*"
  24. regExp.setPattern(pattern1);
  25. int pos = test2.indexOf(regExp);
  26. int matchedLen = regExp.matchedLength();
  27. QStringList capTexts = regExp.capturedTexts();
  28. qDebug()<<pos<<","<<matchedLen<<","<<capTexts;//0,8,("-\r\na-123")
  29.  
  30. //test="-_%\r\na=1abc-_%\r\na=12abc-_%\r\na=123abc-_%\r\na=1234abc",pattern="[a-z]*=\\d*"
  31. regExp.setPattern(pattern2);
  32. //只会找到第一个就停止了
  33. pos = test3.indexOf(regExp);
  34. matchedLen = regExp.matchedLength();
  35. capTexts = regExp.capturedTexts();
  36. qDebug()<<pos<<","<<capTexts.length()<<","<<capTexts;//5,3,1,("a=1")
  37.  
  38. //test="-_%\r\na=1abc-_%\r\na=12abc-_%\r\na=123abc-_%\r\na=1234abc",pattern="[a-z]*=\\d*"
  39. regExp.setPattern(pattern2);
  40. //替换所有,并返回该字符串的引用
  41. qDebug()<<test3.replace(regExp,"xyz");//-_%\r\nxyzabc-_%\r\nxyzabc-_%\r\nxyzabc-_%\r\nxyzabc"
  42. qDebug()<<test3;//-_%\r\nxyzabc-_%\r\nxyzabc-_%\r\nxyzabc-_%\r\nxyzabc"
  43.  
  44. //////////////////////////////////////////////////////////////////////////////////////////////////////
  45.  
  46. //test="hello world,hello qt",pattern="\\w+"
  47. //Since: Qt 5.0
  48. QRegularExpression regularExpression(pattern3);
  49. int index = 0;
  50. QRegularExpressionMatch match;
  51. do {
  52. match = regularExpression.match(test4,index);
  53. if(match.hasMatch()) {
  54. index = match.capturedEnd();
  55. qDebug()<<"("<<match.capturedStart()<<","<<index<<") "<<match.captured(0);
  56. //(0,5) "hello"
  57. //(6,11) "world"
  58. //(13,18) "hello"
  59. //(19,21) "qt"
  60. } else {
  61. break;
  62. }
  63. } while(index < test4.length());
  64.  
  65. //test="a=-\r\na=123abc",pattern="\\w+"
  66. qDebug()<<test2.indexOf(regularExpression);//3
  67.  
  68. //test="hello world,pattern="\\w+"
  69. qDebug()<<test4.replace(regularExpression,"x");//"x x,x x"
  70.  
  71. //正则表达式分组
  72. //test="a=123",pattern="(.+)=(.+)"
  73. regularExpression.setPattern(pattern4);
  74. match = regularExpression.match(test1);
  75. qDebug()<<match.captured(0);//"a=123"
  76. qDebug()<<match.captured(1);//"a"
  77. qDebug()<<match.captured(2);//"123"
  78. qDebug()<<match.capturedTexts();//("a=100","a","100")
  79. return 0;
  80. }

猜你在找的正则表达式相关文章