C++增强:分割字符串

发布于 2024-08-15 06:04:04 字数 490 浏览 3 评论 0原文

如何使用正则表达式使用 Boost 拆分字符串并将分隔符包含在结果列表中?

例如,如果我有字符串“1d2”并且我的正则表达式是“[az]”,我希望结果在带有 (1, d, 2) 的向量中,

我有:

std::string expression = "1d2";
boost::regex re("[a-z]");
boost::sregex_token_iterator i (expression.begin (),
                                expression.end (), 
                                re);
boost::sregex_token_iterator j;
std::vector <std::string> splitResults;
std::copy (i, j, std::back_inserter (splitResults)); 

谢谢

How can I split a string with Boost with a regex AND have the delimiter included in the result list?

for example, if I have the string "1d2" and my regex is "[a-z]" I want the results in a vector with (1, d, 2)

I have:

std::string expression = "1d2";
boost::regex re("[a-z]");
boost::sregex_token_iterator i (expression.begin (),
                                expression.end (), 
                                re);
boost::sregex_token_iterator j;
std::vector <std::string> splitResults;
std::copy (i, j, std::back_inserter (splitResults)); 

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-08-22 06:04:04

我认为你不能直接使用 boost::regex 提取分隔符。但是,您可以提取字符串中找到正则表达式的位置:

std::string expression = "1a234bc";
boost::regex re("[a-z]");
boost::sregex_iterator i(
  expression.begin (),     
  expression.end (),     
  re);
boost::sregex_iterator j;
for(; i!=j; ++i) {
  std::cout << (*i).position() << " : " << (*i) <<  std::endl;
}

此示例将显示:

1:一个

5:b

6:c

使用此信息,您可以从原始字符串中提取分隔符:

std::string expression = "1a234bc43";
boost::regex re("[a-z]");
boost::sregex_iterator i(
  expression.begin (),     
  expression.end (),     
  re);
boost::sregex_iterator j;
size_t pos=0;
for(; i!=j;++i) {
  std::string pre_delimiter = expression.substr(pos, (*i).position()-pos); 
  std::cout << pre_delimiter << std::endl;
  std::cout << (*i) << std::endl;
  pos = (*i).position() + (*i).size();
}
std::string last_delimiter = expression.substr(pos);
std::cout << last_delimiter << std::endl;

此示例将显示:

1

一个

234

b

c

43

b 和 c 之间有一个空字符串,因为没有分隔符。

I think you cannot directly extract the delimiters using boost::regex. You can, however, extract the position where the regex is found in your string:

std::string expression = "1a234bc";
boost::regex re("[a-z]");
boost::sregex_iterator i(
  expression.begin (),     
  expression.end (),     
  re);
boost::sregex_iterator j;
for(; i!=j; ++i) {
  std::cout << (*i).position() << " : " << (*i) <<  std::endl;
}

This example would show:

1 : a

5 : b

6 : c

Using this information, you can extract the delimitiers from your original string:

std::string expression = "1a234bc43";
boost::regex re("[a-z]");
boost::sregex_iterator i(
  expression.begin (),     
  expression.end (),     
  re);
boost::sregex_iterator j;
size_t pos=0;
for(; i!=j;++i) {
  std::string pre_delimiter = expression.substr(pos, (*i).position()-pos); 
  std::cout << pre_delimiter << std::endl;
  std::cout << (*i) << std::endl;
  pos = (*i).position() + (*i).size();
}
std::string last_delimiter = expression.substr(pos);
std::cout << last_delimiter << std::endl;

This example would show:

1

a

234

b

c

43

There is an empty string betwen b and c because there is no delimiter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文