歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++11中正則表達式測試

C++11中正則表達式測試

日期:2017/3/1 10:11:44   编辑:Linux編程

VC++2010已經支持regex了, 可以用來編譯下述代碼.

  1. #include <string>
  2. #include <regex>
  3. #include <iostream>
  4. using namespace std;
  5. /* 測試C++11中的正則表達式. */
  6. int main()
  7. {
  8. //定義正則表達式,匹配時間格式
  9. regex testRegex("[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}");
  10. //要匹配的字符串
  11. string strText("OTA LOG SFTCH/MPA Stream 2/Reservation Accept 07:23:50.580 Channel: 147, Pilot PN: 232");
  12. cmatch result; //結果
  13. //search 是匹配子字符串, match 是匹配整個字符串
  14. if (regex_search(strText.c_str(), result, testRegex, regex_constants::format_default))
  15. {
  16. cout << result.str() << endl;
  17. }
  18. else
  19. {
  20. cout << "fail." << endl;
  21. }
  22. }
Copyright © Linux教程網 All Rights Reserved