c - 正则表达式文件抓取

标签 c regex

我正在使用正则表达式抓取文件中的电子邮件地址。

不幸的是,我的正则表达式规则无法匹配到这个字符串:

" <font size=-1><a href=mailto:mrnours@citeweb.net>_ MR NOURS _</a></font> " ;

我在 stackoverflow 上找不到原因,我希望有人能告诉我我的规则有什么问题。

这是我用来测试它的代码:

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>

int main (void)
{
  int match;
  int err;
  regex_t preg;
  regmatch_t pmatch[5];
  size_t nmatch = 5;
  const char *str_request = "         <font size=-1><a href=mailto:mrnours@citeweb.net>_ MR NOURS _</a></font>          ";

 const char *str_regex = "[a-zA-Z0-9][a-zA-Z0-9_.]+@[a-zA-Z0-9_]+\\.(com|net|[a-zA-Z]{2})$";

  err = regcomp(&preg, str_regex, REG_EXTENDED);
  if (err == 0)
    {
      match = regexec(&preg, str_request, nmatch, pmatch, 0);
      nmatch = preg.re_nsub;
      regfree(&preg);
      if (match == 0)
        {
          printf ("match\n");
          int start = pmatch[0].rm_so;
          int end  = pmatch[0].rm_eo;
          printf("%d - %d\n", start, end);
        }
      else if (match == REG_NOMATCH)
        {
          printf("unmatch\n");
        }
    }
  puts ("\nPress any key\n");
  getchar ();
  return (EXIT_SUCCESS);
 }

最佳答案

我怀疑您正试图将子字符串作为一个完整的单词进行匹配,因此,您在模式的末尾使用了 $(字符串结尾) anchor 。但是,您要查找的子字符串不在输入字符串的末尾。

由于 regex.h 不支持单词边界,您可以使用解决方法:

const char *str_regex = "([a-zA-Z0-9][a-zA-Z0-9_.]+@[a-zA-Z0-9_]+\\.(com|net|[a-zA-Z]{2}))([^a-zA-Z]|$)";
                                                                                          ^^^^^^^^^^^^^

您需要的值将驻留在捕获组 1 中。

查看此 C IDEONE demo :

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>

int main (void)
{
  int match;
  int err;
  regex_t preg;
  regmatch_t pmatch[5];
  size_t nmatch = 4; // We have 4 groups as a result of matching: 0 - the whole match, and 3 capture groups
  const char *str_request = "         <font size=-1><a href=mailto:mrnours@citeweb.net>_ MR NOURS _</a></font>          ";

 const char *str_regex = "([a-zA-Z0-9][a-zA-Z0-9_.]+@[a-zA-Z0-9_]+\\.(com|net|[a-zA-Z]{2}))([^a-zA-Z]|$)";

  err = regcomp(&preg, str_regex, REG_EXTENDED);
  if (err == 0)
    {
      match = regexec(&preg, str_request, nmatch, pmatch, 0);
      nmatch = preg.re_nsub;
      regfree(&preg);
      if (match == 0)
        {
          printf ("match\n");
          int start = pmatch[1].rm_so; // <- Changed from 0 to 1
          int end  = pmatch[1].rm_eo; // <- Changed from 0 to 1
          printf("%d - %d\n\"%.*s\"", start, end, pmatch[1].rm_eo - pmatch[1].rm_so, &str_request[pmatch[1].rm_so]);
        }  //                ^--^ Added a captured substring display
      else if (match == REG_NOMATCH)
        {
          printf("unmatch\n");
        }
    }
  puts ("\nPress any key\n");
  getchar ();
  return (EXIT_SUCCESS);
 }

或者如果您不关心整个单词匹配,只需删除 $

关于c - 正则表达式文件抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36765433/

相关文章:

regex - 正则表达式用于替换字符实例后的所有内容

c - 功能和程序指令存储器

c - 执行 strcpy() 函数后程序崩溃

c - 如何将使用 Windows conio.h 的代码移植到 Linux?

javascript - 爱尔兰 Eircode 验证

javascript - 没有 http ://or www 的 URL 正则表达式

c - 将类型和无效操作数分配给二进制时出现不兼容的类型

c - 我的程序中的 scanfs 和/或 ifs 有问题(咖啡店)

ruby 1.8.7 unicode 正则表达式问题

regex - 正则表达式匹配数字有一个小问题