POSIX正则表达式-方括号表达式是否为零或一匹配项? [英] POSIX regex - zero or one matches of bracket expression?

查看:127
本文介绍了POSIX正则表达式-方括号表达式是否为零或一匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式解析源文件并在C程序中搜索以"LOG"开头的函数,并且可能在或可能不会在类[1248AFM]中添加第二个字符,然后是然后是一个圆括号.这是使用mingw在Windows下开发的,但最终将使用gcc在Linux下进行编译和运行.我正在使用Jan Goyvaerts regex教程作为指南,看来我所追求的是上面显示的方括号表达式的零个或一个匹配项.零或一听起来很像问号元字符,但是在我的实验中,我仍然无法在方括号表达式之后使它起作用.为了说明我正在尝试做的事情,我提供了如下所示的简短程序.理想情况下,我只想在str1和str2上进行匹配.如果我如图所示编译并运行它,那么我什么都找不到.如果我在方括号表达式后遗漏了问号,那么我只会在str2上找到一个匹配项,这是我所期望的.除了问号外,我还尝试了{0,1}形式的间隔量词,但也没有成功.除了方括号表达式之外,我还应该使用其他东西吗?

I'm trying to use regex to parse source files and search for functions in C programs that start with the word "LOG" and may or may not be followed by a second character from the class [1248AFM], which is then followed by an opening parenthesis. This is being developed under Windows using mingw but will ultimately be compiled and run under Linux using gcc. I'm using the Jan Goyvaerts regex tutorial as a guide and it seems like what I'm after is either zero or one matches of the bracket expression expression shown above. Zero or one sounds a lot like the question mark metacharacter but in my experiments I have yet to be able to get that to work following a bracket expression. To illustrate what I'm trying to do I have the short program shown below. Ideally, I would like to have a match on str1 and str2 only. If I compile and run it as shown, I don't get a match on anything. If I leave out the question mark following the bracket expression, I get a match on str2 only, which is what I would expect. In addition to the question mark, I've also tried an interval quantifier of the form {0,1} but had no success with that either. Is there something other than a bracket expression that I should be using?

戴夫

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

int main(int argc, char **argv) {
  regex_t regex;
  int rtn = regcomp(&regex, "LOG[1248AFM]?(", 0);
  if (rtn) {
    printf("compile failed\n");
    return(1);
  }
  char *str1 = "  LOG(";
  char *str2 = "  LOGM(";
  char *str3 = "  LOG";
  char *str4 = "  LOGJ(";

  int rtn1 = regexec(&regex, str1, 0, NULL, 0);
  int rtn2 = regexec(&regex, str2, 0, NULL, 0);
  int rtn3 = regexec(&regex, str3, 0, NULL, 0);
  int rtn4 = regexec(&regex, str4, 0, NULL, 0);
  printf("str1: %d\nstr2: %d\nstr3: %d\nstr4: %d\n",
    rtn1, rtn2, rtn3, rtn4);

  return(0);
}

推荐答案

就像卡西米尔(Casimir)和希波吕特(Hippolyte)所说:您需要转义?,当我发表评论时,该?逃脱了我.问题是您使用字符串文字,这意味着您必须对转义进行转义.

Like Casimir et Hippolyte said: you need to escape the ? which escaped me when I did the comment. The problem is that you use a string literal, that means you have to escape the escape.

编辑,kdhp用户正确地指出:?是基本正则表达式的Gnu扩展.但是问题仍然存在:在C语言中,需要对转义符进行转义.

EDIT as user kdhp noted rightfully: the ? is a Gnu extension to the basic regular expression. But the problem stays the same: the need for escapes of the escapes in a C-literal.

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

int main(int argc, char **argv) {
  regex_t regex;
  // Gnu extension
  // int rtn = regcomp(&regex, "LOG[1248AFM]\\?(",0);
  // Basic regular expression
  int rtn = regcomp(&regex, "LOG[1248AFM]\\{0,1\\}(",0);
  if (rtn) {
    printf("compile failed\n");
    return(1);
  }
  char *str1 = "  LOG(";
  char *str2 = "  LOGM(";
  char *str3 = "  LOG";
  char *str4 = "  LOGJ(";

  int rtn1 = regexec(&regex, str1, 0, NULL, 0);
  int rtn2 = regexec(&regex, str2, 0, NULL, 0);
  int rtn3 = regexec(&regex, str3, 0, NULL, 0);
  int rtn4 = regexec(&regex, str4, 0, NULL, 0);
  printf("str1: %d\nstr2: %d\nstr3: %d\nstr4: %d\n",
    rtn1, rtn2, rtn3, rtn4);

  return(0);
}

给予

str1: 0
str2: 0
str3: 1
str4: 1

这篇关于POSIX正则表达式-方括号表达式是否为零或一匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆