pcre不能支持多个子组
pcre can not support mutiple subgroups
这是关于pcre多个子组的,主题是:
const char* subject = "http://mail.google.com:443";
我想找到协议&域&端口,我的regex是这样的,但pcre_exec返回0
const char* regex_str = "([^/]+)//([^:]+):(\d+)";
但是当这样修改时,pcre_exec返回2:
const char* regex_str = "[^/]+//([^:]+):\d+";
怎么了?
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define VECTORSIZE 6
int main()
{
const char* subject = "http://mail.google.com:443";
const char* regex_str = "([^/]+)//([^:]+):(\d+)";
const char* error = NULL;
int erroffset = 0;
int ovector[VECTORSIZE];
char match[50];
int matchlen = 0;
pcre* regex = pcre_compile(regex_str, PCRE_CASELESS, &error, &erroffset, NULL);
if(regex == NULL)
{
printf("error=%s,offset=%dn", error, erroffset);
return -1;
}
int matches = pcre_exec(regex, NULL, subject, strlen(subject), 0, 0, ovector, VECTORSIZE);
printf("matches=%dn", matches);
if(matches == -1)
{
printf("no matchesn");
return -1;
}
for(int i=0; i<matches; i++)
{
memset(match, 0, sizeof(match));
matchlen = ovector[2*i + 1] - ovector[2*i];
printf("start=%d, lenth=%dn", ovector[2*i], matchlen);
memcpy(match, subject + ovector[2*i], matchlen);
printf("match=%sn", match);
}
pcre_free(regex);
return 0;
}
此行:
#define VECTORSIZE 6
把它改成30左右(int很便宜)就行了。pcre_exec每个子组需要3个元素,整个匹配还需要3个。因此,在您的示例中,它需要至少为12。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的for循环不能正确获取argv
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么模板类中的对象不能返回值
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 数组长度,为什么从命令行获取时不能使用它?
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 警告:不能解析.gnu_debugdata部分;LZMA支持在编译时被禁用
- 不能直接在 C++ 中支持简洁的 int 和字符串?
- 为什么 gcc-4.9.2 不能支持 std::string.insert(迭代器,范围)返回迭代器
- pcre不能支持多个子组
- 不支持的操作.由JRC引擎处理的文档不能在c++堆栈中打开