{
const char *regex = "href=\"[^ >]*\"";
regex_t preg;
const size_t nmatch = 10;
regmatch_t pm[nmatch];
if (regcomp(&preg, regex, 0) != 0) { /*編譯正則表達式失敗 */
perror("regcomp");
exit(1);
}
int z, i;
z = regexec(&preg, buf, nmatch, pm, 0);
if (z == REG_NOMATCH)/*無匹配項 */
{
return 0;
}
else/*有匹配的超鏈接 */
{
for (i = 0; i < nmatch && pm[i].rm_so != -1; ++i)/*把超鏈接都提取出*/
{
/*對匹配鏈接的操作*/
}
}
regfree(&preg);/*釋放正則表達式*/
}
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
#include <string.h>
static char* substr(const char*str, unsigned start, unsigned end)
{
}
int main(int argc, char** argv)
{
{
{
{
{
}
編譯執行
bitwangbin@mac:~/code/c/regex > gcc regexp.c -o regexp
bitwangbin@mac:~/code/c/regex > ./regexp
0003: #include <regex.h>;
0020:
0037: