使用 GNU C 正则表达式库的字符串正则表达式

发布于 2024-08-19 22:52:17 字数 482 浏览 8 评论 0原文

我正在编写一个与 GNU C 正则表达式库一起使用的正则表达式:

字符串的形式为:(斜体文本是内容的描述)

(不是#)开始(可能是空格):数据

我已经编写了以下代码,但它不匹配。

regcomp(&start_state, "^[^#][ \\t]*\\(start\\)[ \\t]*[:].*$", REG_EXTENDED);

我需要写什么?

例子: 匹配:

状态:q0
状态:q0
状态:q0s

不匹配:

#状态:q0
状态 q0
# 状态:q0

谢谢!

I am writing a regex to use with the GNU C regex library:

The string is of the form: (text in italics is a description of content)

(NOT a #) start (maybe whitespace) : data

I have written the following code, but it won't match.

regcomp(&start_state, "^[^#][ \\t]*\\(start\\)[ \\t]*[:].*$", REG_EXTENDED);

What do I need to write?

examples:
to match:

state : q0
state: q0
state:q0s

not to match:

#state :q0
state q0
# state :q0

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉城メ夜风 2024-08-26 22:52:18

好吧,我想通了:

regcomp(&start_state, "^[^#]*[ \\t]*start[ \\t]*:.*$", REG_EXTENDED);

上面解决了我的问题! (事实证明,我忘了在 [^#] 之后加一个 *)...

无论如何,感谢您的帮助,鲁本斯! :)

Ok, I figured it out:

regcomp(&start_state, "^[^#]*[ \\t]*start[ \\t]*:.*$", REG_EXTENDED);

above solves my problem! (turns out, I forgot to put a * after [^#])...

Thanks for your help anyway, Rubens! :)

甜中书 2024-08-26 22:52:18

这适用于您的示例数据:

^[^#]\s*\w+\s*:(?<data>.*?)$

编辑:我不知道,但您需要启用多行支持,如第一个 ^ 和最后一个 $ 与该设置有不同的行为。

This works with your sample data:

^[^#]\s*\w+\s*:(?<data>.*?)$

EDIT: I don't know, but you'll need to enable multiline support, as first ^ and last $ have a different behavior with that setting.

探春 2024-08-26 22:52:17

您问题中的模式将 state 中的第一个字母与 [^#] 一起使用,这导致匹配无法继续,因为它尝试匹配 tate 与模式 \(state\) 相对应。

您传递了标志 REG_EXTENDED,这意味着您不会转义捕获括号,但会转义文字括号。

使用正则表达式,说出您想要匹配的内容:

^[ \\t]*(state)[ \\t]*:.*$

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

int main(int argc, char **argv)
{
  struct {
    const char *input;
    int expect;
  } tests[] = {
    /* should match */
    { "state : q0", 1 },
    { "state: q0",  1 },
    { "state:q0s",  1 },

    /* should not match */
    { "#state :q0",  0 },
    { "state q0",    0 },
    { "# state :q0", 0 },
  };
  int i;
  regex_t start_state;
  const char *pattern = "^[ \\t]*(state)[ \\t]*:.*$";

  if (regcomp(&start_state, pattern, REG_EXTENDED)) {
    fprintf(stderr, "%s: bad pattern: '%s'\n", argv[0], pattern);
    return 1;
  }

  for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
    int status = regexec(&start_state, tests[i].input, 0, NULL, 0);

    printf("%s: %s (%s)\n", tests[i].input,
                            status == 0 ? "match" : "no match",
                            !status == !!tests[i].expect
                              ? "PASS" : "FAIL");
  }

  regfree(&start_state);

  return 0;
}

输出所示:

state : q0: match (PASS)
state: q0: match (PASS)
state:q0s: match (PASS)
#state :q0: no match (PASS)
state q0: no match (PASS)
# state :q0: no match (PASS)

The pattern in your question was consuming the first letter in state with [^#], which left the match unable to proceed because it tries to match tate against the pattern \(state\).

You passed the flag REG_EXTENDED which means you don't escape capturing parentheses but do escape literal parentheses.

With regular expressions, say what you do want to match:

^[ \\t]*(state)[ \\t]*:.*$

as in

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

int main(int argc, char **argv)
{
  struct {
    const char *input;
    int expect;
  } tests[] = {
    /* should match */
    { "state : q0", 1 },
    { "state: q0",  1 },
    { "state:q0s",  1 },

    /* should not match */
    { "#state :q0",  0 },
    { "state q0",    0 },
    { "# state :q0", 0 },
  };
  int i;
  regex_t start_state;
  const char *pattern = "^[ \\t]*(state)[ \\t]*:.*$";

  if (regcomp(&start_state, pattern, REG_EXTENDED)) {
    fprintf(stderr, "%s: bad pattern: '%s'\n", argv[0], pattern);
    return 1;
  }

  for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
    int status = regexec(&start_state, tests[i].input, 0, NULL, 0);

    printf("%s: %s (%s)\n", tests[i].input,
                            status == 0 ? "match" : "no match",
                            !status == !!tests[i].expect
                              ? "PASS" : "FAIL");
  }

  regfree(&start_state);

  return 0;
}

Output:

state : q0: match (PASS)
state: q0: match (PASS)
state:q0s: match (PASS)
#state :q0: no match (PASS)
state q0: no match (PASS)
# state :q0: no match (PASS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文