php preg_match 在 Boost C++中等效?
php preg_match's equivalent in C++ with Boost?
我有以下可用的php代码:
preg_match('/^(d+):/', $string, $matches)
然而,当我尝试在C++中使用Boost的regex库时,它不起作用。
regex expression1("^(\d+):");
std::string filename = "C:\Users\root\Desktop\something.bin";
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
std::string contents((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
cmatch what;
if(regex_match(contents.c_str(), what, expression1))
{
cout<<"value is "<<what[1]<<endl;
}
我不明白为什么。CCD_ 1与其他模式配合得很好,但与这一个CCD_ 2配合得不好。
字符在C字符串中开始转义。尝试
"^(\d+):"
。
| Single quotes | Double quotes |
-----+---------------+----------------+
PHP | string | string |
| (verbatim) | |
-----+---------------+----------------+
C++ | char | string |
-----+---------------+----------------+
在PHP中使用单引号意味着d
没有其特殊含义。
但是,您可以在C++中正确地使用双引号(您必须这样做,因为在C++中,单引号分隔字符,而不是字符串)。但现在您必须考虑令牌d
的特殊含义。
转义反斜杠将解决您的问题:
regex expression("^(\d+):");
如果你使用双引号字符串,你也必须在PHP中这样做:
preg_match("/^(\d+):", $string, $matches);
你做错了。在字符串中使用反斜杠意味着需要对反斜杠进行转义。
尝试"^(\d+):"
(两个反斜杠)
编辑:正如Berry所评论的,PHP单引号字符串中的d
是完全合法的。我错误地认为这实际上是一个违规行为,但PHP会自动"帮助"您修复它。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- Boost::posix_time::ptime舍入到给定的分钟数
- boost xml parsingl将xml的路径作为变量发送