02在字符串中找到连续最长的数字串

02在字符串中找到连续最长的数字串

题目描述

/**
 *  题目描述
 *  在一个字符串中找出连续最长的数字串,并返回这个数字串
 *  如果存在多个,则返回最后一个
 *  如果没有符合条件的数字串,则返回空字符串 ""
 *
 * 注意: 数字串游数字 0-9, 小数点, 正负号组成;
 * 数字串中的小数点,正负号只能出现一次,小数点两边必须是数字,正负号出现在开头且其后必须有数字
 *
 * 输入
 * 1234567890abcd9.+12345.678.9ed
 *
 * 输出
 * +12345.678
 * */

代码


#include <regex>
#include "string"
#include "iostream"
using namespace std;
int main() {
  string str;
  getline(cin, str);  // getline(cin, str)
//  cout << str << endl;

  string pattern = "[+=]?\\d+(\\.\\d+)?"; // pattern ,  [+-]?(\\d)+(\\.\\d)?
  string res;
  for (int i = 0; int(i < str.size()); i++) {
    string strt = str.substr(i); // 从某个字符串开始的子串
    regex reg(pattern);
    smatch match;
    if (regex_search(strt, match, reg)) {
      string matchStr = match.str();
//      cout << matchStr << endl;
      if ( res.size() <= matchStr.size()) { // 保留最后一个最长数字串
        res = matchStr;
      }
    }
  }

  cout << res << endl;
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值