[英]Regex C++: extract substring
我想在另外两个之间提取一个子字符串。
例如: /home/toto/FILE_mysymbol_EVENT.DAT
或者只是FILE_othersymbol_EVENT.DAT
我想得到: mysymbol
和othersymbol
我不想使用 boost 或其他库。 只是来自 C++ 的标准东西,除了 CERN 的 ROOT 库,带有TRegexp ,但我不知道如何使用它......
自去年以来,C++ 已经在标准中内置了正则表达式。 该程序将展示如何使用它们来提取您所追求的字符串:
#include <regex>
#include <iostream>
int main()
{
const std::string s = "/home/toto/FILE_mysymbol_EVENT.DAT";
std::regex rgx(".*FILE_(\\w+)_EVENT\\.DAT.*");
std::smatch match;
if (std::regex_search(s.begin(), s.end(), match, rgx))
std::cout << "match: " << match[1] << '\n';
}
它将输出:
match: mysymbol
但应该注意的是,它在 GCC 中不起作用,因为它的库对正则表达式的支持不是很好。 在 VS2010(可能还有 VS2012)中运行良好,并且应该在 clang 中运行。
到现在(2016 年末),所有现代 C++ 编译器及其标准库都完全符合 C++11 标准,即使不是全部也是 C++14 标准。 GCC 6 和即将到来的 Clang 4 也支持大部分即将到来的 C++17 标准。
与其他正则表达式相比,TRegexp 仅支持非常有限的正则表达式子集。 这使得构建适合您需求的单个正则表达式有些尴尬。
一种可能的解决方案:
[^_]*_([^_]*)_
将匹配字符串直到第一个下划线,然后捕获所有字符直到下一个下划线。 然后在第 1 组中找到匹配的相关结果。
但在你的情况下,为什么要使用正则表达式呢? 只需在字符串中找到第一次和第二次出现的分隔符_
并提取这些位置之间的字符。
如果你想使用正则表达式,我真的推荐使用 C++11 的正则表达式,或者,如果你有一个还不支持它们的编译器,Boost。 Boost 是我认为几乎是标准 C++ 的一部分。
但是对于这个特定的问题,您实际上并不需要任何形式的正则表达式。 在添加所有适当的错误检查( beg != npos
、 end != npos
等)、测试代码并删除我的错别字之后,类似这个草图的东西应该可以正常工作:
std::string between(std::string const &in,
std::string const &before, std::string const &after) {
size_type beg = in.find(before);
beg += before.size();
size_type end = in.find(after, beg);
return in.substr(beg, end-beg);
}
显然,您可以将std::string
更改为模板参数,并且它应该可以与std::wstring
或更很少使用的std::basic_string
实例一起正常工作。
在信任它之前,我会研究极端案例。
但这是一个很好的候选人:
std::string text = "/home/toto/FILE_mysymbol_EVENT.DAT";
std::regex reg("(.*)(FILE_)(.*)(_EVENT.DAT)(.*)");
std::cout << std::regex_replace(text, reg, "$3") << '\n';
一些程序员老兄、Tim Pietzcker 和 Christopher Creutzig 的答案很酷且正确,但在我看来,它们对于初学者来说并不是很明显。
下面的函数是试图为一些程序员老兄和 Tim Pietzcker 的答案创建一个辅助插图:
void ExtractSubString(const std::string& start_string
, const std::string& string_regex_extract_substring_template)
{
std::regex regex_extract_substring_template(
string_regex_extract_substring_template);
std::smatch match;
std::cout << std::endl;
std::cout << "A substring extract template: " << std::endl;
std::cout << std::quoted(string_regex_extract_substring_template)
<< std::endl;
std::cout << std::endl;
std::cout << "Start string: " << std::endl;
std::cout << start_string << std::endl;
std::cout << std::endl;
if (std::regex_search(start_string.begin(), start_string.end()
, match, regex_extract_substring_template))
{
std::cout << "match0: " << match[0] << std::endl;
std::cout << "match1: " << match[1] << std::endl;
std::cout << "match2: " << match[2] << std::endl;
}
std::cout << std::endl;
}
以下重载函数试图帮助说明 Christopher Creutzig 的答案:
void ExtractSubString(const std::string& start_string
, const std::string& before_substring, const std::string& after_substring)
{
std::cout << std::endl;
std::cout << "A before substring: " << std::endl;
std::cout << std::quoted(before_substring) << std::endl;
std::cout << std::endl;
std::cout << "An after substring: " << std::endl;
std::cout << std::quoted(after_substring) << std::endl;
std::cout << std::endl;
std::cout << "Start string: " << std::endl;
std::cout << start_string << std::endl;
std::cout << std::endl;
size_t before_substring_begin
= start_string.find(before_substring);
size_t extract_substring_begin
= before_substring_begin + before_substring.size();
size_t extract_substring_end
= start_string.find(after_substring, extract_substring_begin);
std::cout << "Extract substring: " << std::endl;
std::cout
<< start_string.substr(extract_substring_begin
, extract_substring_end - extract_substring_begin)
<< std::endl;
std::cout << std::endl;
}
这是运行重载函数的主要函数:
#include <regex>
#include <iostream>
#include <iomanip>
int main()
{
const std::string start_string
= "/home/toto/FILE_mysymbol_EVENT.DAT";
const std::string string_regex_extract_substring_template(
".*FILE_(\\w+)_EVENT\\.DAT.*");
const std::string string_regex_extract_substring_template2(
"[^_]*_([^_]*)_");
ExtractSubString(start_string, string_regex_extract_substring_template);
ExtractSubString(start_string, string_regex_extract_substring_template2);
const std::string before_substring = "/home/toto/FILE_";
const std::string after_substring = "_EVENT.DAT";
ExtractSubString(start_string, before_substring, after_substring);
}
这是执行 main 函数的结果:
A substring extract template:
".*FILE_(\\w+)_EVENT\\.DAT.*"
Start string:
"/home/toto/FILE_mysymbol_EVENT.DAT"
match0: /home/toto/FILE_mysymbol_EVENT.DAT
match1: mysymbol
match2:
A substring extract template:
"[^_]*_([^_]*)_"
Start string:
"/home/toto/FILE_mysymbol_EVENT.DAT"
match0: /home/toto/FILE_mysymbol_
match1: mysymbol
match2:
A before substring:
"/home/toto/FILE_"
An after substring:
"_EVENT.DAT"
Start string:
"/home/toto/FILE_mysymbol_EVENT.DAT"
Extract substring:
mysymbol
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.