编写下面的程序是为了使用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) 我想找到所有可能的正则表达式匹配,怎么可能?
regex rx("(2|25)");
string s = "2225";
for (sregex_iterator it(s.begin(), s.end(), rx), end; it != end; ++it) {
cout << it->position() << ": " << it->str() << endl;
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
0: 2
1: 2
2: 25
Run Code Online (Sandbox Code Playgroud)
但是找不到第三名2: 2.我更喜欢使用正则表达式,因为O(n)同时搜索多个令牌的复杂性.
更新:
也许将令牌列表拆分为不可加前缀的列表并创建几个正则表达式?例如:(2|4|25|45|251|455|267)=> ,,(2|4) 这将增加复杂性,像(25|45|267)(251|455)O(n log(m))
更新2:
请提供基于STL的简短算法,将令牌向量拆分为非前缀向量以回答此问题.
使用std ::正则表达式,并给予文件路径,我想只匹配结束的文件名.txt和不是形式_test.txt或.txtTEMP.任何其他下划线都没问题.
所以,例如:
somepath/testFile.txt 应该匹配.somepath/test_File.txt 应该匹配.somepath/testFile_test.txt 不应该匹配.somepath/testFile.txtTEMP 不应该匹配.这种模式的正确正则表达式是什么?
我尝试过的:
(.*?)(\.txt)--->匹配任何以.txt.结尾的文件路径.
要排除包含_test我尝试使用否定外观的文件:
(.*?)(?!_test)(\.txt)
但它没有用.
我也试过负面的lookbehind,但MSVC14(Visual Studio 2015)std::regex_error在创建正则表达式时抛出一个异常,所以我不确定它是否不受支持或者我使用了错误的语法.
我目前正在研究C++预处理器,我需要将字符串常量与超过0个字母匹配"hey I'm a string.我目前正在使用这个,\"([^\\\"]+|\\.)+\"但是我的一个测试用例失败了.
测试用例:
std::cout << "hello" << " world";
std::cout << "He said: \"bananas\"" << "...";
std::cout << "";
std::cout << "\x12\23\x34";
Run Code Online (Sandbox Code Playgroud)
预期产量:
std::cout << String("hello") << String(" world");
std::cout << String("He said: \"bananas\"") << String("...");
std::cout << "";
std::cout << String("\x12\23\x34");
Run Code Online (Sandbox Code Playgroud)
在第二个我反而得到
std::cout << String("He said: \")bananas\"String(" << ")...";
Run Code Online (Sandbox Code Playgroud)
短复制代码(使用AR.3的正则表达式):
std::string in_line = "std::cout << \"He said: \\\"bananas\\\"\" << \"...\";";
std::regex r("\"([^\"]+|\\.|(?<=\\\\)\")+\"");
in_line = std::regex_replace(in_line, r, "String($&)");
Run Code Online (Sandbox Code Playgroud) 我希望能够解决这样的问题:Getting std::ifstream to handle LF, CR, and CRLF? 其中 anistream需要通过复杂的分隔符进行标记;这样标记化的唯一方法istream是:
istream一次读取一个字符正则表达式非常擅长用复杂的分隔符标记字符串:
string foo{ "A\nB\rC\n\r" };
vector<string> bar;
// This puts {"A", "B", "C"} into bar
transform(sregex_iterator(foo.cbegin(), foo.cend(), regex("(.*)(?:\n\r?|\r)")), sregex_iterator(), back_inserter(bar), [](const smatch& i){ return i[1].str(); });
Run Code Online (Sandbox Code Playgroud)
但我不能regex_iterator在 a 上使用 a istream:( 我的解决方案是吸食istream然后运行regex_iterator它,但吸食步骤似乎是多余的。
是否存在istream_iterator和 的邪恶组合regex_iterator,或者如果我想要它,我必须自己编写它吗?