Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从行中提取名称_C++ - Fatal编程技术网

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中的介绍。使用此选项和类似以下的正则表达式:

>.*? (.*?) .*$
  • 获取文本字符
  • *?
    非贪婪搜索任何停在某个空间的东西
    (.*)
    非贪婪搜索或任何停止在空格处但将前面的字符分组的搜索
  • *$
    贪婪搜索,直到字符串结束
有了它,您可以轻松地检查此行是否符合您的条件,同时获取名称。这是一个测试,显示它的工作。对于代码,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