如何从行中提取名称
how can extract the name from a line
本文关键字:提取 更新时间:2023-10-16
假设我有一个我想读的文件的行:
>NZ_FNBK01000055.1 Halorientalis regularis
因此,如何从该行中提取名称,该名称以大于符号开头的线;符号大于符号(不包括在行末尾的新线)的所有内容都是名称。名称应该是:
NZ_FNBK01000055.1 Halorientalis regularis
这是我到目前为止的代码:
bool file::load(istream& file)
{
string line;
while(getline(genomeSource, line)){
if(line.find(">") != string::npos)
{
m_name =
}
}
return true;
}
您可以使用正则表达式轻松处理这两种情况。C 在C 11中引入了<regex>
。使用此和以下等级类似:
>.*? (.*?) .*$
-
>
获取字面角色 -
.*?
非贪婪搜索在空间上停止的任何东西(.*?)
非贪婪搜索sor任何东西都停在一个空间上,但请事先将字符分组。 -
.*$
贪婪搜索直到字符串结束。
这样,您可以轻松地检查此行是否符合您的条件和同时获取名称。这是一个显示其工作的测试。对于代码,C 11 Regex Lib非常简单:
std::string s = ">NZ_FNBK01000055.1 Halorientalis regularis ";
std::regex rgx(">.*? (.*?) .*$"); // Make the regex
std::smatch matches;
if(std::regex_search(s, matches, rgx)) { // Do a search
if (matches.size() > 1) { // If there are matches, print them.
std::cout << "The name is " << matches[1].str() << "n";
}
}
这是一个现场示例。
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- C++从通用引用中提取实际类型
- 从运行服务的应用程序代码中提取窗口句柄
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- std::unordered_map::提取引用/指针失效