在自定义库中使用正则表达式时出错
Error using regex in custom library
我点击了这个链接,它工作得很好。现在我尝试像这样将这个函数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 选项进行编译即可工作。
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 组合正则表达式部分使用 | 不起作用的 C++
- 为什么C++正则表达式这么慢?
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- 在C++中实现正则表达式
- C++正则表达式替换整个单词
- 用C++编写正则表达式的正确方法是什么?
- 如何从Stroustrup的C++书中解释这个正则表达式?
- 使用带有提升/正则表达式的 Rcpp,在 eval(expr, envir, enclos) 中出错:找不到对象 'libboost_regex'
- 在自定义库中使用正则表达式时出错
- 在c++中使用boost正则表达式匹配从字符串转换为整型时出错