C++ 如何从行中提取名称
假设我有一行要读取的文件:C++ 如何从行中提取名称,c++,C++,假设我有一行要读取的文件: >NZ_FNBK01000055.1 Halorientalis regularis 那么,如何从以大于号开头的行中提取名称;大于号后面的所有内容(不包括行尾的换行符)都是名称。 名称应为: NZ_FNBK01000055.1 Halorientalis regularis 以下是我目前的代码: bool file::load(istream& file) { string line; while(getline(genomeSource,
>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中的介绍。使用此选项和类似以下的正则表达式:
>.*? (.*?) .*$
获取文本字符
非贪婪搜索任何停在某个空间的东西*?
非贪婪搜索或任何停止在空格处但将前面的字符分组的搜索(.*)
贪婪搜索,直到字符串结束*$
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";
}
}
std::string s=“>NZ_FNBK01000055.1 Halorientalis regularis”;
std::regex rgx(“>.*?(.*).*$”)//做正则表达式
std::smatch匹配;
如果(std::regex_search(s,matches,rgx)){//执行搜索
如果(matches.size()>1){//如果存在匹配项,则打印它们。
std::cout我想要的名字是'>'后面的所有东西。没有'>'我怎么能得到这个名字呢?比如m_name=这个名字或者coutIt是时候学习它的奇妙功能了。注意,比如,thingy调用了substr
。这个>需要在行的开头吗?你的代码正在搜索一行中的任何地方的>了。如果你只是int在一行开头的a>中,则if条件应为行。find(“>”==0