C++精解【4】

正则表达式

基础

  • 正则表达式,又称规则表达式,(Regular Expression,在代码中常简写为regex、regexp或RE),是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符"),是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串,通常被用来检索、替换那些符合某个模式(规则)的文本。
  • 更多请见百度百科

regex_match

测试正则表达式是否与整个目标字符串相匹配。这是一种完全匹配,不是部分匹配

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <regex>
using namespace std;

vector<string> split(const string &text, char separator) {
    vector<string> tokens;
    stringstream ss(text);
    string item;
    while (getline(ss, item, separator)) {
        if (!item.empty()) {
            tokens.push_back(item);
        }
    }
    return tokens;
}

string removeSpaces(const string& input) {
    string result = input;
    result.erase(std::remove(result.begin(), result.end(), ' '), result.end());
    return result;
}

int main(){
    string codeStr = "x:int=88.11;y:int=11;z:int=99";
    char delimiter = ';';
    vector<string> codeTokens=split(codeStr,delimiter);
    vector<string> symbolTokens,sysmbolVarTokens;
    regex strRx(R"((\d+)(\.)(\d+))");
    smatch match;
    for (const std::string &codeLineToken : codeTokens) {
        delimiter = '=';
        symbolTokens=split(codeLineToken,delimiter);
        delimiter = ':';
        sysmbolVarTokens=split(symbolTokens[0],delimiter);
        if (removeSpaces(sysmbolVarTokens[1])=="int"){
            string intSysbol=removeSpaces(sysmbolVarTokens[0]);
            string value=removeSpaces(symbolTokens[1]);
            bool found = regex_match(value, match, strRx);
            if (found)
                 cout <<"非法赋值" <<intSysbol<<":"<<value<< endl;
            else{
                 cout << "合法赋值" <<intSysbol<<":"<<value<< endl;
            }



        }
   }


    return (0);
}

非法赋值x:88.11
合法赋值y:11
合法赋值z:99

Process returned 0 (0x0)   execution time : 0.214 s
Press any key to continue.

regex_replace

替换匹配正则表达式。
所有大写字母全部用“.”替换。

#include <iostream>
#include <string>
#include <regex>
using namespace std;


int main(){
    string str = "aBjDEIFoo998089";
    regex strRx(R"([A-Z])");
    string fmt(".");
    smatch match;
    std::cout << "replacement == "
        << std::regex_replace(str, strRx, fmt) << std::endl;
    return (0);
   }


replacement == a.j....oo998089

Process returned 0 (0x0)   execution time : 0.220 s
Press any key to continue.

swap

交换两个 basic_regex 或 match_results 对象。

#include <iostream>
#include <string>
#include <regex>
using namespace std;


int main(){
    string str = "a99jDEIFoo998089";
    regex strRx1(R"([A-Z]+)");
    regex strRx2;
    smatch match1;
    smatch match2;
    swap(strRx1, strRx2);
    bool found =regex_search(str, match2, strRx2);
    if (found)
        cout<<match2.str()<<endl;
    return (0);
   }


#include <iostream>
#include <string>
#include <regex>
using namespace std;


int main(){
    string str = "a99jDEIFoo998089";
    regex strRx1(R"([A-Z]+)");
    regex strRx2;
    smatch match1;
    smatch match2;
    swap(strRx1, strRx2);
    bool found =regex_search(str, match2, strRx2);
    swap(match1, match2);
    if (found)
        cout<<match1.str()<<endl;
    return (0);
   }


DEIF

Process returned 0 (0x0)   execution time : 0.197 s
Press any key to continue.

Eigen

概述

  • Eigen是一个用于线性代数的c++模板库:矩阵、向量、数值求解器和相关算法。
  • 除了c++标准库之外,Eigen没有任何依赖关系。
  • 具体见Eigen

简单例子

#include <iostream>
#include "f:/learn/eigen-3.4.0/Eigen/Dense"

using Eigen::MatrixXd;
using Eigen::VectorXd;

int main()
{
  MatrixXd m = MatrixXd::Random(3,3);
  m = (m + MatrixXd::Constant(3,3,1.2)) * 50;
  std::cout << "m =" << std::endl << m << std::endl;
  VectorXd v(3);
  v << 1, 2, 3;
  std::cout << "m * v =" << std::endl << m * v << std::endl;
}

m =
10.1251 90.8741 45.0291
66.3585 68.5009 99.5962
29.3304 57.9873  92.284
m * v =
326.961
502.149
422.157

Process returned 0 (0x0)   execution time : 0.168 s
Press any key to continue.

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值