编写下面的程序是为了使用C++ 11 std :: regex_match&std :: regex_search获取"Day"信息.但是,使用第一种方法返回false,第二种方法返回true(预期).我阅读了与此相关的文档和已存在的SO问题,但我不明白这两种方法之间的区别以及何时应该使用它们中的任何一种?对于任何常见问题,它们都可以互换使用吗?
regex_match和regex_search之间的区别?
#include<iostream>
#include<string>
#include<regex>
int main()
{
std::string input{ "Mon Nov 25 20:54:36 2013" };
//Day:: Exactly Two Number surrounded by spaces in both side
std::regex r{R"(\s\d{2}\s)"};
//std::regex r{"\\s\\d{2}\\s"};
std::smatch match;
if (std::regex_match(input,match,r)) {
std::cout << "Found" << "\n";
} else {
std::cout << "Did Not Found" << "\n";
}
if (std::regex_search(input, match,r)) {
std::cout << "Found" << "\n";
if (match.ready()){
std::string out = match[0];
std::cout …Run Code Online (Sandbox Code Playgroud) 我想在另外两个之间提取一个子串.
前:/home/toto/FILE_mysymbol_EVENT.DAT
或者只是FILE_othersymbol_EVENT.DAT
我希望得到:mysymbol和othersymbol
我不想使用boost或其他库.只是来自C++的标准内容,除了CERN的ROOT lib,还有TRegexp,但我不知道如何使用它...
我想解析Linux中的cpu信息.我写了这样的代码:
// Returns full data of the file in a string
std::string filedata = readFile("/proc/cpuinfo");
std::cmath results;
// In file that string looks like: 'model name : Intel ...'
std::regex reg("model name: *");
std::regex_search(filedata.c_str(), results, reg);
std::cout << results[0] << " " << results[1] << std::endl;
Run Code Online (Sandbox Code Playgroud)
但它返回空字符串.怎么了?
我正在寻找C++ 11中的正则表达式,它可以匹配字符串中的子字符串.
类似的东西: "It´s a dark night out there..."我正在寻找ark
因此,如果子字符串位于字符串中的某个位置,则此表达式应匹配.此外,我想可以选择说这个匹配应该区分大小写.
我已经尝试过了,但似乎没有用......
string str = "It´s a dark night out there...";
regex ex ("ark");
if (regex_match (str,ex))
cout << "Match found!";
Run Code Online (Sandbox Code Playgroud)
有人知道这样的事吗?