zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    Servlet18—— 路径编写总结
    Servlet17—— Cookie
    Servlet16—— 资源跳转
    Servlet15—— 线程安全问题
    Servlet14—— 程序中乱码解决方案
    Servlet13—— ServletRequest
    Servlet12—— GenericServlet
    CF1467B Hills And Valleys
    CF1470B Strange Definition
    【BZOJ1082】【SCOI2005】栅栏
  • 原文地址:https://www.cnblogs.com/maizhongfei/p/14987298.html
Copyright © 2011-2022 走看看