在c项目中使用pcre2

前端之家收集整理的这篇文章主要介绍了在c项目中使用pcre2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找在我的简单c应用程序中使用pcre2,(我正在使用vs2015).
(我正在看各种正则表达式库,一般的感觉是pcre / pcre2是最灵活的)

首先我从官方位置(http://sourceforge.net/projects/pcre/files/pcre2/10.20/)下载了pcre2并创建了一个非常简单的例子.

  1. #define PCRE2_CODE_UNIT_WIDTH 8
  2. #include <pcre2.h>
  3. ...
  4. PCRE2_SPTR subject = (PCRE2_SPTR)std::string("this is it").c_str();
  5. PCRE2_SPTR pattern = (PCRE2_SPTR)std::string("([a-z]+)|\\s").c_str();
  6.  
  7. ...
  8. int errorcode;
  9. PCRE2_SIZE erroroffset;
  10. pcre2_code *re = pcre2_compile(pattern,PCRE2_ZERO_TERMINATED,PCRE2_ANCHORED | PCRE2_UTF,&errorcode,&erroroffset,NULL);
  11. ...

首先文件“pcre2.h”不存在,所以我将pcre2.h.generic重命名为pcre2.h

但后来我得到了未解析的外部链接错误.

我猜我需要从源到项目包含一个或多个文件.
但我不愿意随意添加文件而不知道它们的作用.

有人可以给出一些简单的步骤来使用pcre2成功构建项目吗?

UPDATE
这不是一个导入库问题,pcre2.h没有附带librar(不是我在其发布位置可以看到的那个).

解决方法

如果你不介意使用包装器,这是我的: JPCRE2

您需要根据要使用的字符串类(分别为std :: string,std :: wstring,std :: u16string,std :: u32string)选择基本字符类型(char,wchar_t,char16_t,char32_t):

  1. typedef jpcre2::select<char> jp;
  2. //Selecting char as the basic character type will require
  3. //8 bit PCRE2 library where char is 8 bit,//or 16 bit PCRE2 library where char is 16 bit,//or 32 bit PCRE2 library where char is 32 bit.
  4. //If char is not 8,16 or 32 bit,it's a compile error.

匹配示例:

检查字符串是否与模式匹配:

  1. if(jp::Regex("(\\d)|(\\w)").match("I am the subject"))
  2. std::cout<<"\nmatched";
  3. else
  4. std::cout<<"\nno match";

匹配所有并获得匹配计数:

  1. size_t count =
  2. jp::Regex("(\\d)|(\\w)","mi").match("I am the subject","g");
  3. // 'm' modifier enables multi-line mode for the regex
  4. // 'i' modifier makes the regex case insensitive
  5. // 'g' modifier enables global matching

获取编号的子字符串/捕获的组:

  1. jp::VecNum vec_num;
  2. count =
  3. jp::Regex("(\\w+)\\s*(\\d+)","im").initMatch()
  4. .setSubject("I am 23,I am digits 10")
  5. .setModifier("g")
  6. .setNumberedSubstringVector(&vec_num)
  7. .match();
  8. std::cout<<"\nTotal match of first match: "<<vec_num[0][0];
  9. std::cout<<"\nCaptrued group 1 of first match: "<<vec_num[0][1];
  10. std::cout<<"\nCaptrued group 2 of first match: "<<vec_num[0][2];
  11.  
  12. std::cout<<"\nTotal match of second match: "<<vec_num[1][0];
  13. std::cout<<"\nCaptrued group 1 of second match: "<<vec_num[1][1];
  14. std::cout<<"\nCaptrued group 2 of second match: "<<vec_num[1][2];

获取命名的子串/捕获组:

  1. jp::VecNas vec_nas;
  2. count =
  3. jp::Regex("(?<word>\\w+)\\s*(?<digit>\\d+)","m")
  4. .initMatch()
  5. .setSubject("I am 23,I am digits 10")
  6. .setModifier("g")
  7. .setNamedSubstringVector(&vec_nas)
  8. .match();
  9. std::cout<<"\nCaptured group (word) of first match: "<<vec_nas[0]["word"];
  10. std::cout<<"\nCaptured group (digit) of first match: "<<vec_nas[0]["digit"];
  11.  
  12. std::cout<<"\nCaptured group (word) of second match: "<<vec_nas[1]["word"];
  13. std::cout<<"\nCaptured group (digit) of second match: "<<vec_nas[1]["digit"];

迭代所有匹配和子串:

  1. //Iterating through numbered substring
  2. for(size_t i=0;i<vec_num.size();++i){
  3. //i=0 is the first match found,i=1 is the second and so forth
  4. for(size_t j=0;j<vec_num[i].size();++j){
  5. //j=0 is the capture group 0 i.e the total match
  6. //j=1 is the capture group 1 and so forth.
  7. std::cout<<"\n\t("<<j<<"): "<<vec_num[i][j]<<"\n";
  8. }
  9. }

替换/替换示例:

  1. std::cout<<"\n"<<
  2. ///replace all occurrences of a digit with @
  3. jp::Regex("\\d").replace("I am the subject string 44","@","g");
  4.  
  5. ///swap two parts of a string
  6. std::cout<<"\n"<<
  7. jp::Regex("^([^\t]+)\t([^\t]+)$")
  8. .initReplace()
  9. .setSubject("I am the subject\tTo be swapped according to tab")
  10. .setReplaceWith("$2 $1")
  11. .replace();

替换为匹配评估器:

  1. jp::String callback1(const jp::NumSub& m,void*,void*){
  2. return "("+m[0]+")"; //m[0] is capture group 0,i.e total match (in each match)
  3. }
  4. int main(){
  5. jp::Regex re("(?<total>\\w+)","n");
  6. jp::RegexReplace rr(&re);
  7. String s3 = "I am ঋ আা a string 879879 fdsjkll ১ ২ ৩ ৪ অ আ ক খ গ ঘ আমার সোনার বাংলা";
  8. rr.setSubject(s3)
  9. .setPcre2Option(PCRE2_SUBSTITUTE_GLOBAL);
  10. std::cout<<"\n\n### 1\n"<<
  11. rr.nreplace(jp::MatchEvaluator(callback1));
  12. //nreplace() treats the returned string from the callback as literal,//while replace() will process the returned string
  13. //with pcre2_substitute()
  14.  
  15. #if __cplusplus >= 201103L
  16. //example with lambda
  17. std::cout<<"\n\n### Lambda\n"<<
  18. rr.nreplace(
  19. jp::MatchEvaluator(
  20. [](const jp::NumSub& m1,const jp::MapNas& m2,void*){
  21. return "("+m1[0]+"/"+m2.at("total")+")";
  22. }
  23. ));
  24. #endif
  25. return 0;
  26. }

您可以阅读完整的文档here.

猜你在找的C&C++相关文章