在自定义库中使用正则表达式时出错

Error using regex in custom library

本文关键字:正则表达式 出错 自定义      更新时间:2023-10-16

我点击了这个链接,它工作得很好。现在我尝试像这样将这个函数rreplace放在我自己的库中

myLib.h 的内容

#include <regex>
using namespace std;
class myLib {
    private:
        // Some private things
    public:
        // Some public things
        int rreplace (char *buf, int size, regex_t *re, char *rp);
};

和相应的 myLib.cpp

#include "myLib.h"
// other includes

// other library functions

int rreplace (char *buf, int size, regex_t *re, char *rp){
    char *pos;
    int sub, so, n;
    regmatch_t pmatch [10]; /* regoff_t is int so size is int */
    if (regexec (re, buf, 10, pmatch, 0))
        return 0;
    for (pos = rp; *pos; pos++)
        if (*pos == '' && *(pos + 1) > '0' && *(pos + 1) <= '9'){
            so = pmatch [*(pos + 1) - 48].rm_so;
            n = pmatch [*(pos + 1) - 48].rm_eo - so;
            if (so < 0 || strlen (rp) + n - 1 > size)
                return 1;
            memmove (pos + n, pos + 2, strlen (pos) - 1);
            memmove (pos, buf + so, n);
            pos = pos + n - 2;
        }
    sub = pmatch [1].rm_so; /* no repeated replace when sub >= 0 */
    for (pos = buf; !regexec (re, pos, 1, pmatch, 0); ){
        n = pmatch [0].rm_eo - pmatch [0].rm_so;
        pos += pmatch [0].rm_so;
        if (strlen (buf) - n + strlen (rp) + 1 > size)
            return 1;
        memmove (pos + strlen (rp), pos + n, strlen (pos) - n + 1);
        memmove (pos, rp, strlen (rp));
        pos += strlen (rp);
        if (sub >= 0)
            break;
    }
    return 0;
}

现在当我尝试使用以下方法编译此库时

g++ -c myLib.cpp

它给我抛出了这些我不知道为什么

的错误
In file included from /usr/include/c++/4.6/regex:35:0,
                 from myLib.h:4,
                 from myLib.cpp:1:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
In file included from myLib.cpp:1:0:
myLib.h:36:38: error: ‘regex_t’ has not been declared
myLib.cpp: In member function ‘void myLib::replaceDotsWithCommas()’:
myLib.cpp:18:2: error: ‘regex_t’ was not declared in this scope
myLib.cpp:18:10: error: expected ‘;’ before ‘re’
myLib.cpp:23:12: error: ‘re’ was not declared in this scope
myLib.cpp:23:38: error: ‘REG_ICASE’ was not declared in this scope
myLib.cpp:23:47: error: ‘regcomp’ was not declared in this scope
myLib.cpp: At global scope:
myLib.cpp:114:36: error: ‘regex_t’ has not been declared
myLib.cpp: In function ‘int rreplace(char*, int, int*, char*)’:
myLib.cpp:117:2: error: ‘regmatch_t’ was not declared in this scope
myLib.cpp:117:13: error: expected ‘;’ before ‘pmatch’
myLib.cpp:118:28: error: ‘pmatch’ was not declared in this scope
myLib.cpp:118:37: error: ‘regexec’ was not declared in this scope
myLib.cpp:122:9: error: ‘pmatch’ was not declared in this scope
myLib.cpp:131:8: error: ‘pmatch’ was not declared in this scope
myLib.cpp:132:49: error: ‘regexec’ was not declared in this scope

我正在尝试谷歌它,但没有运气。如果问题太基本,请道歉。

错误消息指出您需要使用选项 -std=c++0x 进行编译。

g++ -std=c++0x -c myLib.cpp

请注意,您使用的编译器版本没有附带正常工作的正则表达式实现。您可以使用 Boost.regex,它应该无需使用 C++11 选项进行编译即可工作。