[英]How to extract specific string from given string in cpp?
I have the following string called header: "bla bla hello, just more characters filename="myfile.1.2.doc" more characters"
我有以下名为header的字符串:
"bla bla hello, just more characters filename="myfile.1.2.doc" more characters"
I need to get the file name and the file type from this string, but my solution seems to be very messy (pseudo code) : 我需要从该字符串中获取文件名和文件类型,但是我的解决方案似乎非常混乱(伪代码):
unsigned int end = header.find("filename=");
unsigned int end2 = header.find(" " ", end + sizeof("filename=") + 1) // how to search for ' " ' ?!
std::string fullFileName = header.substr(end +sizeof("filename=") + 1 ,end2 -1);
//now look for the first "." from the end and split by that .
how to look from the end in cpp? 从cpp到底如何看?
I think It would be better if you use regular expressions. 我认为如果使用正则表达式会更好。
For example: we have more complicated string with a few file names and confusing characters like (") outside the file name. 例如:我们有更复杂的字符串,其中包含几个文件名,而文件名外的字符(如(“))令人困惑。
std::string str("bla bla hello, just more characters filename=\"myfile.1.2.doc\" more characters bla bla hello, just more characters filename=\"newFile.exe\" more char\"acters");
std::smatch match;
std::regex regExp("filename=\"(.*?)\\.([^.]*?)\"");
while (std::regex_search(str, match, regExp))
{
std::string name = match[1].str();
std::string ext = match[2].str();
str = match.suffix().str();
}
The first iteration gives you: 第一次迭代为您提供:
name = myfile.1.2 名称= myfile.1.2
ext = doc ext = doc
The second: 第二:
name = newfile 名称= newfile
ext = exe ext = exe
size_t startpos = header.find("filename=");
if (startpos != header.npos)
{ // found filename
startpos += sizeof("filename=") - 1; // sizeof determined at compile time.
// -1 ignores the null termination on the c-string
if (startpos != header.length() && header[startpos] == '\"')
{ // next char, if there is one, should be "
startpos++;
size_t endpos = header.find('\"', startpos);
if (endpos != header.npos)
{ // found terminating ". get full file name
std::string fullfname = header.substr(startpos, endpos-startpos);
size_t dotpos = fullfname.find_last_of('.');
if (dotpos != fullfname.npos)
{ // found dot split string
std::string filename = fullfname.substr(0, dotpos);
//add extra brains here to remove path
std::string filetype = fullfname.substr(dotpos + 1, token.npos);
// dostuff
std::cout << fullfname << ": " << filename << " dot " << filetype << std::endl;
}
else
{
// handle error
}
}
else
{
// handle error
}
}
else
{
// handle error
}
}
else
{
// handle error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.