• C++ string trim / erase / to_lowercase 及正则表达式 match / search / replace


    #include <iostream>
    #include <strstream>
    #include <string>
    #include <iomanip>
    #include <vector>
    #include <map>
    #include <set>
    #include <stack>
    #include <array>
    #include <unordered_map>
    #include <unordered_set>
    #include <algorithm>
    #include <functional>
    #include <queue>
    #include <array>
    #include <cmath>
    #include <regex>
    using namespace std;
    
    #include "ListNode.h"
    #include "TreeNode.h"
    
    int main() {
    	auto ltrim = [](string& s) {
    		s.erase(s.begin(), find_if(s.begin(), s.end(), [](unsigned char c) { return !std::isspace(c); }));
    	};
    	auto rtrim = [](string& s) {
    		s.erase(find_if(s.rbegin(), s.rend(), [](unsigned char c) { return !std::isspace(c); }).base(), s.end());
    	};
    	auto trim = [ltrim, rtrim](string& s) {
    		ltrim(s);
    		rtrim(s);
    	};
    
    	cout << "---- demo for string trim:" << endl;
    	string str = "   Hello World!   ";
    	ltrim(str);
    	cout << "ltrim : " << "/"  << str << "/" << endl;
    
    	str = "   Hello World!   ";
    	rtrim(str);
    	cout << "rtrim : " << "/"  << str << "/" << endl;
    
    
    	str = "   Hello World!   ";
    	trim(str);
    	cout << "trim : " << "/"  << str << "/" << endl;
    
    	str = "   Hello World!   ";
    	auto erase_all = [](string& str) {
    		str.erase(remove(str.begin(), str.end(), ' '), str.end());
    		return str;
    	};
    	cout << "erase_all : " << erase_all(str) << endl;
    
    	cout << endl;
    	cout << "---- demo for regex_match :" << endl;
    	cout << "is number? " << std::boolalpha  << regex_match("123A", regex("^\d+$")) << endl;
    
    	cout << endl;
    	cout << "---- demo for regex_search :" << endl;
    
    	cout << "-- search one" << endl;
    
    	string s = "AA01 + AB00 * 00BB";
    	smatch sm;
    	auto r = regex("([A-Z]{2})(\d{2})");
    	cout << std::boolalpha << regex_search(s, sm, r) << endl;
    	for (auto i = 0; i < sm.size(); i ++) {
    		cout << "[" << i << "]" << " = " << sm[i] << endl;
    	}
    
    	cout << "-- search all" << endl;
    
    	auto to_lowercase = [](string s) {
    		std::transform(s.begin(), s.end(), s.begin(), [](char c) { return std::tolower(static_cast<unsigned char>(c)); });
    		return s;
    	};
    	while (regex_search(s, sm, r))
    	{
    		std::cout << sm.str() << " --> " << sm[2] << to_lowercase(sm[1]) << '
    ';
    		s = sm.suffix();
    	}
    
    	cout << endl;
    	cout << "---- demo for regex_replace :" << endl;
    	s = "AB02 AB03";
    	r = regex("([A-Z]{2})(\d{2})");
    	cout << regex_replace(s, r, "$& ==> $2$1 , ") << endl;
    
    	return 0;
    }
    

      output:

    ---- demo for string trim:
    ltrim : /Hello World!   /
    rtrim : /   Hello World!/
    trim : /Hello World!/
    erase_all : HelloWorld!
    
    ---- demo for regex_match :
    is number? false
    
    ---- demo for regex_search :
    -- search one
    true
    [0] = AA01
    [1] = AA
    [2] = 01
    -- search all
    AA01 --> 01aa
    AB00 --> 00ab
    

      

  • 相关阅读:
    HTML中的文本标签
    Java 数组的创建
    JavaScript实现LUHN算法验证银行卡号有效性
    JavaScript实现HTML页面集成QQ空间分享功能
    JavaScript中的三种弹出框的区别与使用
    Maven 项目中的 pom.xml 文件内容说明
    FTPClient 中 FTPClient.changeWorkingDirectory(filePath) 代码一直返回 false
    Eclipse 中 Debug 时鼠标悬停无法查看变量值
    Innodb ,MyISAM
    tomcat jetty
  • 原文地址:https://www.cnblogs.com/maizhongfei/p/14987298.html
Copyright © 2020-2023  润新知