C++ [正则表达式] 如何提取给定的字符值
c++ [regex] how to extract given char value
如何提取数字值?
std::regex legit_command("^\([A-Z]+[0-9]+\-[A-Z]+[0-9]+\)$");
std::string input;
假设用户输入
(AA11-BB22)
我要得到
first_character = "aa"
first_number = 11
secondt_character = "bb"
second_number = 22
您可以使用捕获组。在下面的示例中,我用(AA11-BB22)
替换了(AA11+BB22)
以匹配您发布的正则表达式。请注意,只有当整个字符串与模式匹配时,regex_match
才会成功,因此不需要行断言的开头/结尾(^
和$
(。
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
const string input = "(AA11-BB22)";
const regex legit_command("\(([A-Z]+)([0-9]+)-([A-Z]+)([0-9]+)\)");
smatch matches;
if(regex_match(input, matches, legit_command)) {
cout << "first_character " << matches[1] << endl;
cout << "first_number " << matches[2] << endl;
cout << "second_character " << matches[3] << endl;
cout << "second_number " << matches[4] << endl;
}
}
输出:
$ c++ main.cpp && ./a.out
first_character AA
first_number 11
second_character BB
second_number 22
相关文章:
- 从 std::vector<无符号字符>切片中提取 int?
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- Getline(); 提取以 1A 字符停止
- 如何从字符数组C++中提取2个整数并将它们存储在2个变量中(初学者)
- 将文本文件数据读入字符数组时提取运算符的歧义
- C++ [正则表达式] 如何提取给定的字符值
- 如何从输入字符串中提取特定位置的字符?
- 从字符串中提取单个字符并将其转换为 int
- 提取字符后返回到文件中的特定位置
- 将字符数组的一部分提取为 char*
- 正则表达式:从模式字符串 1.string2'string3 中提取字符串,其中字符串 1 可以包含 '." 字符
- 如何用c++打印从文本文件中提取的非英文字符
- 如何将提取的字符串数据转换为字符数组
- 如何从两个字符之间的文件中提取整数
- C++ - 从整数数组中插入和提取字符
- C++,从字符数组中提取有符号的多位数整数
- 如何提取C 中的字符/字符串之间的字符串
- 如何在重载提取运算符中读取行(忽略空格字符)
- 从QString中提取字符并进行比较
- 字符提取与字符串提取有何不同