C++ Boost库 操作字符串与正则
字符串的查找与替换一直是C++的若是,运用Boost这个准标准库,将可以很好的弥补C++的不足,使针对字符串的操作更加容易。
字符串格式转换:
#include <iostream>
#include <Windows.h>
#include <boost\lexical_cast.hpp>
#include <string>
using namespace std;
using namespace boost;
int main(int argc, char * argv[])
{
string str[3] = { "100", "102", "3.14159" };
// 字符串转换为数值类型
std::cout << "字符串转为整数: " << lexical_cast<int>(str[0]) << std::endl;
std::cout << "字符串转为长整数: " << lexical_cast<long>(str[1]) << std::endl;
std::cout << "字符串转为浮点数: " << lexical_cast<float>(str[2]) << std::endl;
// 数值类型转化为字符串
std::cout << "数值转为字符串: " << lexical_cast<string>(100) << std::endl;
std::cout << "十六进制转为十进制: " << lexical_cast<string>(0x10) << std::endl;
system("pause");
return 0;
}
format格式化:
#include <iostream>
#include <Windows.h>
#include <string>
#include <boost\format.hpp>
using namespace std;
using namespace boost;
int main(int argc, char * argv[])
{
// 第一种输出方式: 直接填充字符串
boost::format fmtA("姓名: %s -> 年龄: %d -> 性别: %s");
fmtA %"lyshark";
fmtA % 22;
fmtA %"男";
std::string str = fmtA.str();
std::cout << "格式化后: " << str << std::endl;
// 第二种方式: 拷贝的使用
boost::format fmtB("姓名: %s -> 年龄: %d -> 性别: %s");
cout << format(fmtB) % "lyshark" % 23 % "男" << std::endl;
system("pause");
return 0;
}
string_algo 大小写转化函数
#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost;
int main(int argc, char * argv[])
{
string str("readme.log");
if (ends_with(str, "log"))
{
cout << "转化为大写: " << to_upper_copy(str) << endl;
cout << "转化为小写: " << to_lower_copy(str) << endl;
}
// 替换开头
replace_first(str, "readme", "lyshark");
std::cout << "替换开头: " << str << std::endl;
// 执行删除后缀
vector<char> v(str.begin(), str.end());
vector<char> v2 = to_upper_copy(erase_first_copy(v, ".log"));
for (auto x : v2)
{
cout << x ;
}
system("pause");
return 0;
}
判断类
#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost;
int main(int argc, char * argv[])
{
string str[6] = { "hello lyshark", "hello LyShark", "ABC", "ABC", "FCE" ,"lyshark"};
cout << "大小写不敏感判断后缀: " << iends_with(str[0], "lyshark") << endl;
cout << "大小写敏感判断前缀: " << starts_with(str[1], "Hello") << endl;
cout << "测试包含关系: " << contains(str[0], str[5]) << endl;
cout << "测试前5个字母是否为小写: " << all(str[0].substr(0, 5), is_lower()) << endl;
is_any_of()
system("pause");
return 0;
}
修正
#include <iostream>
#include <string>
#include <boost\format.hpp>
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost;
int main(int argc, char * argv[])
{
string str[3] = { "hello LyShark", "hello LyShark", "lyshark" };
cout << "大小写不敏感判断后缀: " << iends_with(str[0], "lyshark") << endl;
cout << "大小写敏感判断前缀: " << starts_with(str[1], "Hello") << endl;
cout << "测试包含关系: " << contains(str[0], str[2]) << endl;
cout << "前5个字母是否为小写: " << all(str[0].substr(0, 5), is_lower()) << endl;
cout << "前2个字符是否为字母: " << all(str[1].substr(0,2), is_alpha()) << endl;
// 字符串修剪 去空格
boost::format fmt("|%s|\n");
std::string my_string = " hello lyshark ";
cout << "删除两端空格: " << fmt %trim_copy(my_string) << endl;
cout << "删除左端空格: " << fmt %trim_left_copy(my_string) << endl;
trim_right(my_string);
cout << "原地删除右端空格: " << fmt %my_string << endl;
// 字符串修建 去特殊符号
std::string my_stringa = "2021 happy new Year !!!";
cout << "删除左端数字: " << fmt %trim_left_copy_if(my_stringa, is_digit()) << endl;
cout << "删除右端标点: " << fmt %trim_right_copy_if(my_stringa, is_punct()) << endl;
cout << "删除两端(标点|数字|空格): " << trim_copy_if(my_stringa, is_punct() || is_digit() || is_space()) << endl;
system("pause");
return 0;
}
字符串查找
#include <iostream>
#include <string>
#include <boost\format.hpp>
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
boost::format fmt("|%s|. pos = %d\n");
std::string my_string = "Long long ago, there was Ago king as long.";
iterator_range<std::string::iterator> reg; // 定义迭代区间
// 寻找第一次出现的位置(大小写敏感)
reg = find_first(my_string, "Ago");
cout << fmt %reg % (reg.begin() - my_string.begin()) << endl;
// 寻找最后一次出现的位置(大小写不敏感)
reg = ifind_last(my_string, "ago");
cout << fmt %reg % (reg.begin() - my_string.begin()) << endl;
// 寻找第三次出现long的位置(大小写不敏感)
reg = ifind_nth(my_string, "long", 2);
cout << fmt %reg % (reg.begin() - my_string.begin()) << endl;
// 取前四个字符和后四个字符
reg = find_head(my_string, 4);
cout << fmt %reg % (reg.begin() - my_string.begin()) << endl;
reg = find_tail(my_string, 4);
cout << fmt %reg % (reg.begin() - my_string.begin()) << endl;
// 找不到则输出
reg = find_first(my_string, "lyshark");
cout << "flag = " << (reg.empty() && !reg) << endl;
getchar();
return 0;
}
替换与删除 注意带有_copy的为拷贝,代表可以使用string变量来接收,不带的直接操作原始字符串。
#include <iostream>
#include <string>
#include <boost\format.hpp>
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
boost::format fmt("|%s|. pos = %d\n");
std::string my_string = "Long long ago, there was Ago king as long.";
// 替换开头字符串(两种替换方式)
std::string str_copy = replace_first_copy(my_string, "long", "LONG");
cout << "替换后返回字符串: " << str_copy << endl;
replace_first(my_string, "ago", "AGO");
cout << "直接替换在原始字符串上: " << my_string << endl;
// 替换开头或结尾前后5个字符
replace_tail(my_string, 5, "lyshark");
cout << "替换结尾5个字符: " << my_string << endl;
replace_head(my_string, 5, "lyshark");
cout << "替换开头5个字符: " << my_string << endl;
// 替换第一次出现long的位置为AGES
replace_nth(my_string, "long", 0, "AGES");
cout << "第一次出现位置: " << my_string << endl;
// 替换所有出现过的位置
std::string str_copy_a = replace_all_copy(my_string, "lyshark", "LYSSHARK");
cout << "替换所有位置: " << str_copy_a << endl;
// 删除第1次出现was位置的字符串
std::string del_str_copy = erase_nth_copy(my_string, "was", 0);
cout << "删除后的字符串: " << del_str_copy << endl;
// 删除字符串中所有的LYSSHARK
erase_all(my_string, "LYSSHARK");
cout << "删除后的字符串: " << my_string << endl;
getchar();
return 0;
}
切割合并字符串:
#include <iostream>
#include <string>
#include <algorithm>
#include <boost\algorithm\string.hpp>
#include <boost\assign.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
std::string my_string = "lyshark,Link.Zelda:Mario-Ligui+zelda,ZELDA";
std::string my_string_b("hello||lyshark||welcome||link");
// 查找字符串中的特定字符串
deque<std::string> deq;
ifind_all(deq, my_string, "zelda");
cout << "查找字符串个数(不区分大小写): " << deq.size() << endl;
if (deq.size() == 3)
{
for (auto each : deq)
cout << "[ " << each << " ]" << endl;
}
// 切割字符串(1)
list<iterator_range<std::string::iterator>> ptr;
split(ptr, my_string, is_any_of(",.:-+"));
for (auto each : ptr)
cout << "[ " << each << " ]" << endl;
// 切割字符串(2)
ptr.clear();
split(ptr, my_string, is_any_of(".:-"), token_compress_on);
for (auto each : ptr)
cout << "[ " << each << " ]" << endl;
// 合并字符串
std::vector<string> vct;
vct.push_back("hello");
vct.push_back("lyshark");
cout << "用空格将其连接起来: " << join(vct, " ") << endl;
// 查找迭代器的使用
typedef find_iterator<string::iterator> string_find_iterator;
string_find_iterator pos, end;
for (pos = make_find_iterator(my_string_b, first_finder("lyshark", is_iequal())); pos != end; ++pos)
{
cout << "[ " << *pos << " ]" << endl;
}
// 分割迭代器的使用
typedef split_iterator<string::iterator> string_split_iterator;
string_split_iterator p, endp;
for (p = make_split_iterator(my_string_b, first_finder("||", is_iequal())); p != endp; ++p)
{
cout << "[ " << *p << " ]" << endl;
}
getchar();
return 0;
}
正则模块的使用:
正则匹配
#include <iostream>
#include <string>
#include <algorithm>
#include <boost\algorithm\string.hpp>
#include <boost\xpressive\xpressive.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
using namespace boost::xpressive;
// 简单匹配字符串
cregex regxA = cregex::compile("a.c");
cout << "匹配字符串: " << regex_match("abd", regxA) << endl;
std::string StringA = "hello lyshark a.c";
cregex regxD = cregex::compile("a.c");
cout << "匹配字符串: " << regex_match((char *)&StringA, regxD) << endl;
// 数组方式引用正则
cregex_compiler regc;
regc["regxA"] = regc.compile("a|b|c");
regc["regxB"] = regc.compile("\\d*");
cout << regex_match("abcdefg", regc["regxA"]) << endl;
cout << regex_match("123123", regc["regxB"]) << endl;
// 使用C++ 11 匹配身份证
cregex regxB = cregex::compile(R"---(\d{6}(1|2)\d{3}(0|1)\d[0-3]\d\d{3}(X|\d))---", icase);
cout << "验证身份证: " << regex_match("513436200002247099", regxB) << endl;
// 使用C++ 98 匹配身份证
cregex regxC = cregex::compile("\\d{6}((1|2)\\d{3})((0|1)\\d)([0-3]\\d)(\\d{3}(X|\\d))", icase);
cmatch what;
regex_match("513436200002247099", what, regxC);
for (auto &each : what)
{
cout << "[ " << each << " ]" << endl;
}
cout << "生日为: " << what[1] << what[3] << what[5] << endl;
// 正则匹配开头结尾
string StringB("lyshark.log");
sregex start_regx = sregex::compile("^ly.*");
cout << "匹配开头: " << regex_match(StringB, start_regx) << endl;
sregex end_regx = sregex::compile(".*log$");
cout << "匹配结尾: " << regex_match(StringB, end_regx) << endl;
getchar();
return 0;
}
正则查找替换
#include <iostream>
#include <string>
#include <algorithm>
#include <boost\algorithm\string.hpp>
#include <boost\xpressive\xpressive.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
using namespace boost::xpressive;
// 正则查找特定字符串
char my_stringA[] = "This is power-studio territory";
cregex regx = cregex::compile("(power)-(.{6})", icase);
cmatch what;
regex_search(my_stringA, what, regx);
if (what.size() != 0)
{
for (int x = 0; x < what.size(); x++)
{
cout << what[x] << endl;
}
}
// 正则替换特定字符串
std::string my_stringB = "2020 Happy New Year !!!";
sregex regxA = sregex::compile("^(\\d| )*"); // 匹配开头数字
sregex regxB = sregex::compile("!*$"); // 匹配末尾标点符号
cout << regex_replace(my_stringB, regxA, "2021") << endl;
cout << regex_replace(my_stringB, regxB, "") << endl;
getchar();
return 0;
}
正则迭代与分词
#include <iostream>
#include <string>
#include <algorithm>
#include <boost\algorithm\string.hpp>
#include <boost\xpressive\xpressive.hpp>
#include <boost\xpressive\regex_iterator.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
using namespace boost::xpressive;
// 正则迭代输出
std::string my_string_a = "power-shell, power-studio,power-engine,super-user";
sregex regxA = sregex::compile("power-(\\w{5})", icase);
sregex_iterator start_ptr(my_string_a.begin(), my_string_a.end(), regxA);
sregex_iterator end_ptr;
for (; start_ptr != end_ptr; ++start_ptr)
{
cout << "[ " << (*start_ptr)[0] << " ]" << endl;
}
// 正则分词
char my_string_b[] = "*lyshark*||+administrator+||root!!||metaper";
cregex regxB = cregex::compile("\\w+", icase); // 过滤出所有的字母
cregex_token_iterator pos(my_string_b, my_string_b + strlen(my_string_b), regxB);
for (; pos != cregex_token_iterator(); ++pos)
{
cout << "[ " << *pos << " ]" << endl;
}
// 正则切割
cregex split_regx = cregex::compile("\\|\\|");
pos = cregex_token_iterator(my_string_b, my_string_b + strlen(my_string_b), split_regx, -1);
for (; pos != cregex_token_iterator(); ++pos)
{
cout << "[ " << *pos << " ]" << endl;
}
// 正则格式化(小写转大写)
struct formater
{
string operator()(cmatch const &m) const
{
return boost::to_upper_copy(m[0].str());
}
};
char my_string_c[] = "*lyshark*||+administrator+||root!!||metaper";
cregex regxC = cregex::compile("\\w+", icase);
cout << regex_replace(my_string_c, regxC, formater()) << endl;
getchar();
return 0;
}
分词器使用:
#include <iostream>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
std::string strTag = "explorer.exe,1024";
// 定义分割符号为逗号与空格 定义分词器
boost::char_separator<char> sep(", ");
typedef boost::tokenizer<boost::char_separator<char>> CustonTokenizer;
CustonTokenizer tok(strTag, sep);
// 将分词结果放入vector链表
std::vector<std::string> vecSegTag;
for (CustonTokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
vecSegTag.push_back(*beg);
}
// const_case 将string转换为char 8
std::string ref_string = const_cast<char *>(vecSegTag[0].c_str());
cout <<"进程名: " << ref_string << endl;
cout << "分词数: " << vecSegTag.size() << endl;
getchar();
return 0;
}
C++ Boost库 操作字符串与正则的更多相关文章
- (三)Boost库之字符串处理
(三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i : 有这个前缀表名算法的大小写不 ...
- (二)boost库之字符串格式化
(二)boost库之字符串格式化 程序中经常需要用到字符串格式化,就个人而言还是比较倾向于C格式的输出,如果只是打印日志,printf就够了,如果到生成字符串,获取你可以选择sprintf,但这些都是 ...
- boost库:字符串处理
使用boost库的字符串处理之前,需要进行区域设置.类:std::locale,每个C++程序自动拥有一个此类的实例,不能直接访问全局区域设置. 全局区域设置可以使用类std::locale中的静态函 ...
- Boost库初见
Boost库是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++库,有C++"准"标准库的美称! Boost有着与其它程序库(如MFC等)无法比拟的优点. Boost库采用了 ...
- 漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)
就在近日,Facebook宣布开源了内部使用的C++底层库,总称folly,包括散列.字符串.向量.内存分配.位处理等,以满足大规模高性能的需求. 这里是folly的github地址:https:// ...
- 使用boost库生成 随机数 随机字符串
#include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...
- boost库(条件变量)
1相关理念 (1)类名 条件变量和互斥变量都是boost库中被封装的类. (2)条件变量 条件变量是thread库提供的一种等待线程同步的机制,可实现线程间的通信,它必须与互斥量配合使用,等待另一个线 ...
- boost库在工作(39)网络UDP异步服务端之九
前面创建的UDP服务器和客户端,都是同步的方式,也就是说当接收数据时,不能参与别的事情执行的.如果在一个只有界面线程的程序里,又不想创建多线程,导致复杂程度的增加,在这种情况之下,我们还有一个方案可以 ...
- (九)boost库之文件处理filesystem
(九)boost库之文件处理filesystem filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...
- (一)boost库之日期、时间
(一)boost库之日期.时间 一.计时器 计时器,通常在一个项目中统计一个函数的执行时间是非常实用的. #include <boost/timer.hpp> void PrintU ...
随机推荐
- Grafana--添加用户
版本:6.5.2 添加用户: 设置账号密码: 设置账号权限(新增的用户都是仅查看的权限):
- DNS--安装&&配置文件
1 下载 #下载服务yum -y install bind#下载解析工具yum -y install bind-utils 2 配置文件 主配置文件 /etc/named.conf 区配置文件 /va ...
- 数据探索之道:查询Web API数据中的JSON字符串列
前言 在当今数据驱动的时代,对数据进行探索和分析变得愈发关键.Web API作为广泛应用的数据源,提供了丰富的信息和资源.然而,面对包含JSON字符串列的Web API数据时,我们常常遇到一个挑战:如 ...
- AtCoder ABC 165 D - Floor Function (Good, 手动模拟推出公式)
题目链接:Here 题意: 给出正整数 \(A,B,N (1\le A\le 1e6,1\le B,N\le1e12)\) ,对于 \(x\in [0,N]\) 求出 \(\left\lfloor\f ...
- AtCoder Regular Contest 121 (AB题解)
补题链接:Here A - 2nd Greatest Distance 二维坐标图中有 \(n\) 个房子,现在规定两房子距离定义为: \(max(|x_i−x_j|,|y_i−y_j|)\) 求第二 ...
- InnoDB 事务加锁分析
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/S7MhlsZveBHRSQhq5aTIJA作者:何志创 一般大家对数据库事务的了解可能停留在事 ...
- 智慧风电:数字孪生 3D 风机智能设备运维
前言 6 月 1 日,福建省人民政府发布关于<福建省"十四五"能源发展专项规划>的通知.规划要求,加大风电建设规模.自 "30·60" 双碳目标颁布 ...
- docker构建java镜像,运行镜像出现 no main manifest attribute, in /xxx.jar
背景 本文主要是一个随笔,记录一下出现"no main manifest attribute"的解决办法 问题原因 主要是近期在构建一个镜像,在镜像构建成功后,运行一直提示&quo ...
- 【ThreadX-USBX】Azure RTOS USBX概述
Azure RTOS USBX是高性能USB主机,设备和移动(OTG)嵌入式堆栈.Azure RTOS USBX与Azure RTOS ThreadX完全集成,并且可用于所有ThreadX支持的处理器 ...
- [转帖]前后台切换命令(ctrl+z jobs bg fg &)
当我在终端里面运行某个命令的时候,结果不是很快就能出来的那种,或者是一大堆字在屏幕上狂翻.这个时候,有时ctrl+c也不起作用,那我会用ctrl+z退出来,这个很有效,但是说实话我不知道为什么这个可以 ...