gpt4 book ai didi

c++ - 正则表达式问题

转载 作者:行者123 更新时间:2023-11-28 03:59:31 26 4
gpt4 key购买 nike

我有以下字符串:

const std::string args = "cmdLine=\"-d ..\\data\\configFile.cfg\" rootDir=\"C:\\abc\\def\""; // please note the space after -d

我想把它分成 2 个子字符串:

std::str1 = "cmdLine=...";

std::str2 = "rootDir=...";

使用 boost/algorithm/string.hpp 。我想,正则表达式最适合这个,但不幸的是我不知道如何构建一个因此我需要问这个问题。

谁能帮我解决这个问题?

最佳答案

要解决您的问题,最简单的方法是使用 strstr在字符串中查找子字符串,string::substr复制子字符串。但是如果你真的想使用 Boost 和正则表达式,你可以像下面的例子那样做:

#include <boost/regex.hpp>

...

const std::string args = "cmdLine=\"-d ..\\data\\configFile.cfg\" rootDir=\"C:\\abc\\def\"";
boost::regex exrp( "(cmdLine=.*) (rootDir=.*)" );
boost::match_results<string::const_iterator> what;
if( regex_search( args, what, exrp ) ) {
string str1( what[1].first, what[1].second ); // cmdLine="-d ..\data\configFile.cfg"
string str2( what[2].first, what[2].second ); // rootDir="C:\abc\def"
}

关于c++ - 正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com