如何在 c++ 中regex_match #(哈希符号)
How to regex_match # (hash symbol) in c++?
我正在阅读.C 和 .C++中的 CPP 文件。目前使用 BOOST 进行recursive_directory迭代器。
我的问题是我尝试使用正则表达式来提取 C 文件中的 #define 变量。 但是正则表达式根本不匹配。如何regex_match(#define???
注意:正则表达式只是标准::正则表达式,而不是 boost::正则表达式
代码片段:
//input stream file
ifstream inFile(path.string(),ios_base::in);
//reading file line by line
while(getline(inFile,readLine))
{
//Excluding comments
if(regex_match(readLine,regex("\/|;|\*")))
{
//ignore do nothing
}
else if(regex_match(readLine,regex("#")))
{
cout<<"regex # matched"
}
尝试regex_match(readLine,regex("#"), regex_match(readLine,regex("^#"), regex_match(readLine,regex("^\#")--> # 不是特别的,但仍然只是想检查regex_match(readLine,regex(".#")
没有什么比这更好的..任何人都可以帮忙???
编辑:IM使用VS2012,视窗8
更新:".#."效果好...谢谢大家
在这里更新,以便像我这样有疑问的每个人都可以轻松查看它.. 谢谢
尝试正
则表达式模式
"#.*"
这种模式将匹配一个 #,后跟任意数量的字符,直到换行符,并且适用于我的所有测试用例(尽管很少),假设我答对了问题。
这是文档说:
The entire target sequence must match the regular expression for this function
to return true (i.e., without any additional characters before or after the match).
For a function that returns true when the match is only part of the sequence,
see regex_search.
所以它应该是下面的一个与整个字符串匹配:
else if(regex_match(readLine,regex(".*#.*")))
或者,如果#
在行首。
else if(regex_match(readLine,regex("^#.*")))
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用Qt C++计算类似Git的SHA1哈希
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- c++找不到具有相同哈希的无序集合元素
- 哈希文件递归并保存到矢量Cryptopp中
- 对 pair<pair<int,int>pair<int,int unordered_map进行哈希处理>>
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 如何为字符串生成唯一但一致的 N 位哈希(小于 64 位)?
- 定义宏后的哈希符号
- C++ concurrent_unordered_map C++11 Microsoft 使用无符号长作为密钥/哈希函数
- 对于具有 3 个无符号字符和一个 int 的结构来说,对于unordered_map来说,什么是好的哈希函数?
- gcc 预处理的表单哈希符号 + 数字"# 1"输出行是什么意思?
- 在 C/C++ 预处理器中,单个井号/哈希符号 (#) 在自己的行上有什么用
- 迁移一个哈希函数,其中包含从 C++/Qt 到 Java 的 32 位无符号整数
- 三个有符号整数的哈希函数
- 试图创建一个for循环来显示从1(#)到8(#)的哈希符号
- 使用哈希表构建符号表
- 如何在 c++ 中regex_match #(哈希符号)