c++ smatch结构
时间: 2023-07-23 13:03:12 浏览: 68
`smatch` 是 C++ 标准库中的一个类,用于存储和操作正则表达式匹配的结果。它是 `<regex>` 头文件中 `std::smatch` 类的实例化对象。
`smatch` 类提供了访问和操作正则表达式匹配结果的方法。它通常与 `regex_match` 或 `regex_search` 函数结合使用,用于执行字符串与正则表达式之间的匹配操作。
以下是一个简单示例,演示如何使用 `smatch` 对象存储匹配结果:
```cpp
#include <iostream>
#include <regex>
int main() {
std::string input = "Hello, World!";
std::regex pattern("Hello");
std::smatch match;
if (std::regex_search(input, match, pattern)) {
std::cout << "Match found: " << match.str() << std::endl;
} else {
std::cout << "No match found." << std::endl;
}
return 0;
}
```
在上面的示例中,我们使用 `std::regex_search` 函数在字符串 `input` 中搜索与正则表达式 `pattern` 匹配的内容。如果找到匹配项,将会将结果存储在 `match` 对象中。然后,我们可以使用 `match.str()` 方法来访问匹配到的字符串。
请注意,`smatch` 对象可以存储多个匹配结果,例如如果正则表达式中使用了捕获组。我们可以通过索引访问每个匹配到的子字符串,例如 `match[0]`、`match[1]` 等。
希望这个简单的示例对你有所帮助!如果有任何问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)