如何用C语言编写一个合适的模式
how to write a appropriate pattern in pcre with C
现在我有一个字符串,它有许多子字符串,如"href="http://www.AAA.com"和其他字符,这里是我的问题,在我的C代码中我写:
char pattern[] = "/^href.*>$/g";
,我想取长字符串中的所有url。但这行不通。有人能帮帮我吗?感谢你的帮助。代码如下:
#define PCRE_STATIC //
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src[] = "<a href="http://union.elong.com/r/hotel/2000000000855850825" target="_blank">ss</a></td></tr><tr><td><a href="http://123.sogou.com/sub/fanyi.html" targedd</a></td><td><a href="http://123.sogou.com/sub/fantizi.html" target="_blank">繁 体 字</a></td><td><a href="http://123.sogou.com/sub/kuaidi.htm>快递查询</a></td></tr><tr><td><a href="http://q.stock.sohu.com/index.shtm>股票行情</a></td><td><a href="http://www.chinamobile.com/service/billservice/>话费查询</a></td><td><a href="http://auto.sohu.com/s2004/weizhangchaxun.shtml>交通违章</a></td></tr><tr><td>";
char pattern[] = "/^href.*>$/g";
re = pcre_compile(pattern,
0,
&error,
&erroffset,
NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %sn", erroffset, error);
return 1;
}
rc = pcre_exec(re,
NULL,
src,
strlen(src),
0,
PCRE_MULTILINE,
ovector,
OVECCOUNT);
if (rc < 0) {
if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...n");
else printf("Matching error %dn", rc);
pcre_free(re);
return 1;
}
printf("nOK, %d has matched ...nn",rc);
for (i = 0; i < rc; i++) {
char *substring_start = src + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("$%2d: %.*sn", i, substring_length, substring_start);
}
pcre_free(re);
return 0;
}
试试这个正则表达式。
myregexp = pcre_compile("href\s*=\s*(['"])(.*?)\1", 0, &error, &erroroffset, NULL);
示例代码:
pcre *myregexp;
const char *error;
int erroroffset;
int offsetcount;
int offsets[(2+1)*3]; // (max_capturing_groups+1)*3
myregexp = pcre_compile("href\s*=\s*(['"])(.*?)\1", 0, &error, &erroroffset, NULL);
if (myregexp != NULL) {
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (2+1)*3);
while (offsetcount > 0) {
// match offset = offsets[0];
// match length = offsets[1] - offsets[0];
if (pcre_get_substring(subject, &offsets, offsetcount, 0, &result) >= 0) {
// Do something with match we just stored into result
}
offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, offsets[1], offsets, (2+1)*3);
}
} else {
// Syntax error in the regular expression at erroroffset
}
相关文章:
- 具有多个继承共享一个资源的对象 - 寻找良好的设计模式
- 对于存储另一个类所需信息的类,例如其构造,是否有设计模式?
- 将终端设置为原始模式:仅在按下下一个键后显示字符
- 为什么这些 CRTP 模式中只有一个可以编译?
- 如何在另一个线程中关闭 MFC 模式对话框并获取对话框返回值?
- 无法在硬件模式下创建 SGX 安全区 - "invalid launch token"即使文档将无效的启动令牌指定为第一个
- QlineEdit:显示一个处理过的文本,而不是输入的文本,而是保留它(自定义回声模式)
- 为什么装饰器模式实现需要一个具有核心类的公共抽象超类
- 为什么CRTP(奇怪的递归模板模式)试图选择另一个私有基类的另一个同名函数
- 为什么将好奇模板模式的基类直接转换为另一个基类是错误的?
- 如何创建一个新过程,而没有从管理模式运行的MFC应用程序中的管理特权
- 在数组中查找一个或多个模式
- 正则表达式匹配是否保证始终只关注最后一个模式?C++
- 在匹配另一个模式的字符串中找到最短子字符串的开始和结尾索引
- 下一个最大的整数,有一些中间位与模式匹配?
- 获取二进制文件的所有可能变体 - 给定一个模式
- 两个附带的类层次结构-一个好的设计模式
- 模板化的使用是否为具有自定义键的unordered_map/set创建了一个良好的模式
- 在找到模式后检索上一个和下一个模式
- ReadProcessMemory找到一个模式粒度