c++ - 如何使用 regex_search 获取报价中的内容
问题描述
我试图在双引号内获取文本。到目前为止,我只能使用 qoutation 进行打印。我正在使用 regex_search。
文件.txt
"Life is Hard, stay calm." - someone
主文件
#include <iostream>
#include <string>
#include <regex>
#include <fstream>
using namespace std;
int main(){
ifstream fOpen("file.txt");
string line0;
getline(fOpen,line0);
regex pattern2("(.*) -.*");
smatch matcher;
regex_search(line0, matcher, pattern2);
cout<<matcher[1]<<endl;
return 0;
}
到目前为止的输出
"Life is Hard, stay calm."
感谢您的帮助
解决方案
试试这个: \"([^\"]*)\"
演示在这里
代码:
int main(){
ifstream fOpen("file.txt");
string line0;
getline(fOpen,line0);
regex pattern2("\"([^\"]*)\"");
smatch matcher;
regex_search(line0, matcher, pattern2);
cout<<matcher[1]<<endl;
return 0;
}
推荐阅读
- angular-material - Angular Material:奇怪的后缀功能
- elasticsearch - Elasticsearch bucket_selector 包括父聚合大小
- java - 在java中拆分字符串的问题
- powerbi - Power BI - TopoJSON 的形状图渲染问题
- excel - 运行时错误“91”:对象变量或未设置块
- swift - SwiftUI:如何在内容视图中显示函数的输出
- python - Webscraping - 我需要一些帮助来理解如何区分页面上的项目 BS4,请求
- c++ - 在 Windows Server 2019 上,如何让两个 MFC 应用程序使用窗口句柄和 GetDC() 进行通信?
- reporting-services - SSRS 提示输入凭据 HTTPS 但不是 HTTP
- reactjs - Reactjs:使用 libarchivejs 处理 7z 文件:Archive.open() 被冻结并且没有给出错误或结果