没有躲过的坑--正则表达式截取字符串

工程中,需要从字符串中匹配出以:开头,并以:结束的字符串。

Google还是百度,很多C++的正则表达式都是通过st::tr1或boost库中使用的,但是我们仅仅用一个小小的功能,就用一个库不是很好的办法。

对的,之前我的博客已经介绍了C++11的新特性-正则表达式。

所以可以不使用其他的库,来完成任务:

std::vector<string> all_sub_string = {};
std::string all_string = "12:wo:sfd:wom::sdf";

std::regex e(":[a-z0-9_+-]+:");//正则规则

const std::sregex_token_iterator end;

for (std::sregex_token_iterator i(all_string .begin(), all_string .end(), e); i != end; ++i)
{
    all_sub_string .push_back(*i);
}

你可能会迷惑,什么是sregex_token_iterator?
不要着急,sregex_token_iterator其实就是字符串 regex_token_iterator 的类型定义。

typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;

上面的方法很简单,就像使用迭代器一样。

其实regex还有其他的查找方法,现在介绍一下regex_search:
Returns whether some sub-sequence in the target sequence (the subject) matches the regular expression rgx (the pattern). The target sequence is either s or the character sequence between first and last, depending on the version used.

直接上代码:

// regex_search example
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;

  while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}

输出:

Target sequence: this subject has a submarine as subsequence
Regular expression: /\b(sub)([^ ]*)/
The following matches and submatches were found:
subject sub ject
submarine sub marine
subsequence sub sequence
  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一苇渡江694

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值