正则表达式编程实例

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

1.使用c++的正则表达式替换对应内容

  1. std::string sKey = it->first;
  2. std::string sPattern = "(<)(/)?(" + sKey + ")(>)";
  3. std::regex rPattern(sPattern);
  4.  
  5. std::string sReplace = "$1$2" + it->second + "$4";
  6. sMsg = std::regex_replace(sMsg,rPattern,sReplace);
sKey为要查找的关键词。sPattern为关键词加上正则格式后的字符串,"(<)(/)?(" + sKey + ")(>)",第一个()中表示有一个"<",第二个()后的?表示在<后是否存在?。整体意思为查"<heros1>","</heros1>"这样的字符串。 sReplace为匹配串模式 "$1$2" + it->second + "$4" 表示第1,2,4个单元串不会参与到替换。

2.找出所有的坐标点

  1. std::smatch rPotRet;
  2. std::regex rPotPattern("[(]([0-9]+),([0-9]+)[)]");
  3. const std::sregex_token_iterator end;
  4.  
  5. for (std::sregex_token_iterator itPot(sMsg.begin(),sMsg.end(),rPotPattern); itPot != end; ++itPot)
  6. {
  7. std::string sPot = *itPot;
  8. if (std::regex_search(sPot,rPotRet,rPotPattern))
  9. {
  10. CPoint pot;
  11. pot.x = atoi(rPotRet[1].str().c_str());
  12. pot.y = atoi(rPotRet[2].str().c_str());
  13. vecPot.push_back(pot);
  14. }
  15. }


"[(]([0-9]+),([0-9]+)[)]":[(]为必有一个(;[0-9]+表示有若干个0-9的数。整个意思就是查找 "(20,89)" ,“(1,22)”这样的字符串。

代码

  1. // regex1.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <regex>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <map>
  10. #include "Windows.h"
  11. #include "Windef.h"
  12. #include "atltypes.h"
  13.  
  14. typedef std::map<std::string,std::string> MapColorType;
  15. MapColorType GmapColor;
  16.  
  17. void mapColorInit()
  18. {
  19. GmapColor.insert(MapColorType::value_type("heros1","12FFFGSEVF"));
  20. GmapColor.insert(MapColorType::value_type("heros2","22FDGRG7"));
  21. GmapColor.insert(MapColorType::value_type("location","24FDGRG7"));
  22. }
  23.  
  24. std::string regexDeal(std::string sMsg,MapColorType mapColor,std::vector<CPoint>& vecPot)
  25. {
  26. std::string sRet;
  27. for (MapColorType::iterator it = mapColor.begin(); it != mapColor.end(); it++)
  28. {
  29. std::string sKey = it->first;
  30. std::string sPattern = "(<)(/)?(" + sKey + ")(>)";
  31. std::regex rPattern(sPattern);
  32.  
  33. std::string sReplace = "$1$2" + it->second + "$4";
  34. sMsg = std::regex_replace(sMsg,sReplace);
  35.  
  36. if (sKey == "location")
  37. {
  38. std::smatch rPotRet;
  39. std::regex rPotPattern("[(]([0-9]+),rPotPattern))
  40. {
  41. CPoint pot;
  42. pot.x = atoi(rPotRet[1].str().c_str());
  43. pot.y = atoi(rPotRet[2].str().c_str());
  44. vecPot.push_back(pot);
  45. }
  46. }
  47. }
  48. }
  49. return sMsg;
  50. }
  51.  
  52. int main()
  53. {
  54. mapColorInit();
  55.  
  56. std::vector<CPoint> vecPot;
  57. std::string text = "<heros1>sixi</heros1><location>(11,11)</location><location>(22,22)</location>";
  58. std::string sRet = regexDeal(text,GmapColor,vecPot);
  59. std::cout <<"Input:" << text << std::endl;
  60. std::cout << "Out:"<<sRet << std::endl;
  61.  
  62. for (std::vector<CPoint>::iterator it = vecPot.begin(); it != vecPot.end(); it++)
  63. {
  64. std::cout << it->x << std::endl;
  65. std::cout << it->y << std::endl;
  66. }
  67. return 0;
  68. }

运行效果

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