GCC 正则表达式

发布于 2024-10-20 09:00:42 字数 143 浏览 8 评论 0原文

如何在 GNU G++ / GCC 中使用正则表达式来匹配、搜索和替换子字符串?例如,您可以提供有关 regex_t 和其他内容的教程吗?

谷歌搜索一个多小时没有给我提供任何可以理解的教程或手册。

How do I use regular expressions in GNU G++ / GCC for matching, searching and replacing substrings? E.g. could you provide any tutorial on regex_t and others?

Googling for above an hour gave me no understandable tutorial or manual.

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

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

发布评论

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

评论(3

我很OK 2024-10-27 09:00:42

我强烈建议使用 Boost C++ 正则表达式库。如果您正在开发严肃的 C++,Boost 绝对是您必须考虑的因素。

该库支持 Perl 和 POSIX 正则表达式语法。我个人更喜欢 Perl 正则表达式,因为我相信它们更直观且更容易正确。

http://www.boost。 org/doc/libs/1_46_0/libs/regex/doc/html/boost_regex/syntax.html

但是如果您对这个优秀的库没有任何了解,我建议您从这里开始:

http://www.boost.org/doc/libs/1_46_0 /libs/regex/doc/html/index.html

I strongly suggest using the Boost C++ regex library. If you are developing serious C++, Boost is definitely something you must take into account.

The library supports both Perl and POSIX regular expression syntax. I personally prefer Perl regular expressions since I believe they are more intuitive and easier to get right.

http://www.boost.org/doc/libs/1_46_0/libs/regex/doc/html/boost_regex/syntax.html

But if you don't have any knowledge of this fine library, I suggest you start here:

http://www.boost.org/doc/libs/1_46_0/libs/regex/doc/html/index.html

乖不如嘢 2024-10-27 09:00:42

我在此处找到了答案:

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

int main() 
{
  int r;
  regex_t reg;

  if (r = regcomp(®, "\\b[A-Z]\\w*\\b", REG_NOSUB | REG_EXTENDED)) 
  {
    char errbuf[1024];

    regerror(r, ®, errbuf, sizeof(errbuf));
    printf("error: %s\n", errbuf);

    return 1;
  }

  char* argv[] = { "Moo", "foo", "OlOlo", "ZaooZA~!" };

  for (int i = 0; i < sizeof(argv) / sizeof(char*); i++) 
  {
    if (regexec(®, argv[i], 0, NULL, 0) == REG_NOMATCH)
      continue;

    printf("matched: %s\n", argv[i]);
  }

  return 0;
}

上面的代码将为我们提供

matched: Moo 
matched: OlOlo 
matched: ZaooZA~!

I found the answer here:

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

int main() 
{
  int r;
  regex_t reg;

  if (r = regcomp(®, "\\b[A-Z]\\w*\\b", REG_NOSUB | REG_EXTENDED)) 
  {
    char errbuf[1024];

    regerror(r, ®, errbuf, sizeof(errbuf));
    printf("error: %s\n", errbuf);

    return 1;
  }

  char* argv[] = { "Moo", "foo", "OlOlo", "ZaooZA~!" };

  for (int i = 0; i < sizeof(argv) / sizeof(char*); i++) 
  {
    if (regexec(®, argv[i], 0, NULL, 0) == REG_NOMATCH)
      continue;

    printf("matched: %s\n", argv[i]);
  }

  return 0;
}

The code above will provide us with

matched: Moo 
matched: OlOlo 
matched: ZaooZA~!
梦幻的心爱 2024-10-27 09:00:42

手册应该很容易找到:POSIX 正则表达式函数。如果您不明白这一点,我强烈建议您尝试温习 C 和 C++ 技能。

请注意,一旦找到匹配项,实际替换子字符串是一个完全不同的问题,正则表达式函数无法帮助您解决这个问题。

Manuals should be easy enough to find: POSIX regular expression functions. If you don't understand that, I would really recommend trying to brush up on your C and C++ skills.

Note that actually replacing a substring once you have a match is a completely different problem, one that the regex functions won't help you with.

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