C 正则是从脚本文件中提取变量
C++ regex to extract variables from a scripting file
所以我有一个脚本文件,其中使用#符号分配了一些变量,然后是分配操作员(=),然后是字符串。所有这些都是没有空间的。有时,会有评论(从符号开始)或一些额外的空格。例如:
#mat=3 !mat denotes a material number
我想使用C 的Regex实用程序来提取'#mat'和'3'。我无法弄清楚正则模式。即使我有模式,我也不知道如何专门从该行提取" #mat"answers" 3"。当我将COUT用于Regex_search的Smatch阵列时,我会得到整个行。
有什么建议吗?非常感谢您的帮助/建议。
#include <regex>
#include <iostream>
int main()
{
const std::string s = "#mat=3 !mat denotes a material number";
std::regex rgx("#(\w+)=(\d+)");
std::smatch match;
if (std::regex_search(s.begin(), s.end(), match, rgx))
std::cout << match[1]<<" "<<match[2]<< 'n';
}
组0代表整个正则:#(\w+)=(\d+)
组1,2表示正则第一个和第二个子组:(\w+)
和(\d+)
因此,match[0]
将返回正则竞赛,而match[1]
将返回REGEX的第一个子组,即(\w+)
,match[2]
将返回ROGEX的第二个子组,即(\d+)
\d+
是指匹配,直到字数字中的最后一次出现。
输出是mat 3
。
使用C 14。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何从也包含C++字母的文本文件中提取某些数字?
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- JPG-如何从ICC配置文件部分APP2读取/提取数据
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 如何使用 WINAPI 和 C++ 提取可执行文件的文件描述?
- 查找并提取文件中的某个值
- 如何从txt文件c++中提取括号之间的数字
- 在Linux Mint 19下,从哪里提取wxWidgets的源代码(.cpp文件)用于步骤内部调试
- 如何将ASCII数字转换为从文件中提取的整数
- 帧提取 使用OpenCV的视频文件开头只有几秒钟
- 从 C++ 中的文件路径中提取文件名
- 将文本文件数据读入字符数组时提取运算符的歧义
- 从.o目标文件中提取函数的原始机器代码?
- 有谁知道ziplib是否能够在不实际提取所有文件的情况下验证zip库
- 是否可以为给定的静态库人为地诱导对象文件提取
- C++将.reg文件提取到注册表
- c++文件提取检查是否为int
- 使用文件提取操作符读取int或string