有人可以解释两者之间的差异吗?现在学习哪个更好?知识将如何从一个转移到另一个,反之亦然?
我有这些变量:
boost::regex re //regular expression to use
std::string stringToChange //replace this string
std::string newValue //new value that is going to replace the stringToChange depending on the regex.
Run Code Online (Sandbox Code Playgroud)
我只想替换它的第一次出现.
谢谢费拉斯.
编辑:我发现了这个:
boost::regex_replace(stringToChange, re, boost::format_first_only);
Run Code Online (Sandbox Code Playgroud)
但它说功能不存在,我猜这些参数目前是不正确的.
我目前正在尝试通过KIT编译Contraction Hierachies实现,这需要Boost :: Regex.提供的Makefile已经确保(我也手动仔细检查过)g ++随-lboost_regex交换机提供.如果没有安装库,g ++会抱怨.
所以我从我的包源安装了库,并再次尝试编译.这次我得到了很多关于Boost :: Regex的链接器错误.这是一个简短的摘录:
main.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb]+0x2c): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `void boost::re_detail::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0x7d): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xb1): undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xcb): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.o: In function `__gnu_cxx::__normal_iterator<char const*, std::string> boost::re_detail::re_is_set_member<__gnu_cxx::__normal_iterator<char const*, std::string>, char, boost::regex_traits<char, boost::cpp_regex_traits<char> >, unsigned int>(__gnu_cxx::__normal_iterator<char …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用boost :: iostreams将以下bash代码转换为C++:
#!/usr/bin/bash
(
gzip -cd file1.ext.gz
cat file2.ext
) | grep '^regex' # or sed 's/search/replace/'
Run Code Online (Sandbox Code Playgroud)
我可以打开一个文件并解压缩它:
std::ifstream s("file.ext.gz", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_istreambuf in;
in.push(boost::iostreams::gzip_decompressor());
in.push(s);
Run Code Online (Sandbox Code Playgroud)
然后打开一个未压缩的文件:
std::ifstream s2("file.ext", std::ios_base::in | std::ios_base::binary);
Run Code Online (Sandbox Code Playgroud)
现在我有点卡住了,所以这是我的问题:
1)什么是boost :: iostreams解决方案来连接两个流?
2)如何通过正则表达式过滤器输出结果来模拟grep/sed?
因此,我想要一个可以复制到cout的istream:
boost::iostream::copy(result, std::cout);
Run Code Online (Sandbox Code Playgroud)
使用Hamigaki的连接更新完整解决方案:
/*
* convert the following bash script into C++
*
* #!/bin/bash
* (
* gzip -cd file1.ext.gz
* cat file2.ext
* ) | grep '^filter' | 'sed s/search/replace/g'
*
*/
#include <iostream>
#include …Run Code Online (Sandbox Code Playgroud) 你如何使用boost::regex_searchC++中的忽略大小写标志或常量?
请发一个简单的例子.
谢谢!
我正在使用C++ 11的<regex>支持,并想检查字符串的开头是否与正则表达式匹配.[如果有帮助我可以切换到Boost,但我的印象是它们基本相同.]
显然,如果我能控制表达式的实际文本表示,我可以^在它的开头作为锚点.
但是,如果我只有一个regex(或basic_regex)对象怎么办?我可以修改它代表的正则表达式来添加锚吗?或者我必须使用regex_search,得到结果,并检查它是否从0位开始?
我试图在C++中获取一个字符串并查找其中包含的所有IP地址,并将它们放入一个新的矢量字符串中.
我已经阅读了很多关于正则表达式的文档,但我似乎无法理解如何做这个简单的函数.
我相信我可以使用这个Perl表达式来查找任何IP地址:
re("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b");
Run Code Online (Sandbox Code Playgroud)
但我仍然难以理解如何做其余的事情.
如果数据流中有趣的数据,我正在尝试匹配一些块.
应该有一个前导的<四个字母数字字符,两个校验和字符(或者??如果没有指定shecksum)和一个尾随字符>.
如果最后两个字符是字母数字,则以下代码按预期工作.如果他们??失败了.
// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data??>bble";
// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;
// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false
Run Code Online (Sandbox Code Playgroud)
我没有在文档中发现任何暗示应该是这种情况的东西(除了NULL和换行符应该匹配AIUI).
那么我错过了什么?
我正在尝试编译我在linux上使用正则表达式的程序.我通过输入make -fgcc.mak在libs/regex/build中构建了boost库,它创建了一个目录gcc,其中包含以下四个文件
boost_regex-gcc-1_35
boost_regex-gcc-d-1_35
libboost_regex-gcc-1_35.a
libboost_regex-gcc-d-1_35.a
Run Code Online (Sandbox Code Playgroud)
现在我想使用我的程序中的正则表达式,它位于某个任意目录中.我#include boost/regex.hpp
我收到的错误表明找不到regex.hpp.然后我在g ++编译器中给出了-I选项.我没有得到那个错误.但是我收到以下错误
undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
Run Code Online (Sandbox Code Playgroud)
我用谷歌搜索,发现我需要以某种方式将上述4个库中的一个链接到我的程序.我该怎么做.我应该链接哪一个?为什么?
我试图在Linux机器上安装Boost(CentOs,Linux版本2.6.9-67.ELsmp).我按照Boost 入门网页上的说明进行操作.下载并解压缩文件,然后我添加前缀,因为我不是root用户.
./bootstrap.sh --prefix=/my_path/boost-1.49.0
./b2 install
Run Code Online (Sandbox Code Playgroud)
我转到/my_path/boost-1.49.0并检查那里有/ include/boost /和/ lib /包含文件.
我也将LD_LIBRARY_PATH设置为/my_path/boost-1.49.0/lib/.
要测试我是否成功安装,我编译以下代码:
main.cpp中
#include <boost/regex.hpp>
#include <iostream>
#include <string>
using namespace boost;
using namespace std;
int main(int argc, char *argv[])
{
string line = "12345";
regex pattern("^123");
if (regex_match(line, pattern)) cout << "match." << endl;
else cout << "not match." << endl;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
并按命令编译:
g++ -v -I /my_path/boost-1.49.0/include/boost -L /my_path/boost-1.49.0/lib main.cpp -lboost_regex -o example
Run Code Online (Sandbox Code Playgroud)
并生成以下错误:
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared …Run Code Online (Sandbox Code Playgroud)