C++字符串中获得指定的子串

从字符串中获得指定的子串C++

  1. 预处理字符串,通过特定字符将原字符串切片处理为多个子串
std::vector<std::string> StringSplit(const std::string &original_string, const char t)
{
    std::vector<std::string> ret_split;
    if (original_string.empty())
    {
        return ret_split;
    }

    const size_t len = original_string.length();
    size_t found = 0u, last_index = 0u;
    std::string temp;
    while ((found = original_string.find(t, last_index)) != (size_t)-1)
    {
        temp = original_string.substr(last_index, found - last_index);
        if (!temp.empty())
        {
            ret_split.push_back(temp);
        }
        last_index = found + 1u;
    }
    if (last_index < len)
    {
        temp = original_string.substr(last_index, found - last_index);
        if (!temp.empty())
        {
            ret_split.push_back(temp);
        }
    }
    return ret_split;
}
  1. 从各子串中查找指定的字符串
std::string GetSubString(std::string &original_string)
{
    std::string specified_string= "";
    if (original_string.empty())
    {
        return specified_string;
    }
    std::vector<std::string> substrings = StringSplit(original_string, '\n');
    const std::regex specified_pattern("_AB(\\d{8})_");
    std::smatch specified_match;
    for (const std::string &sub : substrings)
    {
        if (std::regex_search(sub, specified_match, specified_pattern))
        {
            specified_string= specified_match[0];
            size_t pos = specified_string.find("_");
            while (pos != std::string::npos)
            {
                specified_string.erase(pos, 1u);
                pos = specified_string.find("_");
            }
            break;
        }
    }
    return specified_string;
}
  1. 语法补充

substr
在 C++ 中,std::string 类提供了一个成员函数 substr,该函数用于提取字符串的子串。
**substr函数有两种常用的重载形式:
std::string substr(size_t pos = 0) const;
std::string substr(size_t pos = 0, size_t len) const;
第一种形式从位置 pos开始提取字符串的剩余部分
第二种形式从位置 pos 开始提取长度为 len 的子串。
参数说明:
pos:指定子串开始的索引位置(默认为0,即字符串的开头)
len:指定要提取的子串的长度
返回值: 返回一个新的 std::string 对象,包含提取的子串

size_t find(const std::string& str, size_t pos = 0) const;
这个函数在找到子字符串str 时返回第一次出现的位置(从索引 pos 开始搜索),如果没有找到则返回std::string::npos,std::string::npos 是一个特殊的常量,通常被定义为 size_t类型的最大值,表示一个无效的索引,例子中用(size_t)-1代替,通常用std::string::npos表示比较安全

substr(pos, std::string::npos);
这行代码会从 str 的索引 pos 开始提取到字符串末尾的所有字符,也就是例子中的original_string.substr(last_index, found - last_index);
->original_string.substr(last_index ,std::string::npos);

const std::regex specified_pattern(“AB(\d{8})”);
在 C++ 中,std::regex 用于定义正则表达式,而 specified_pattern是一个正则表达式的实例
这个正则表达式"AB(\d{8})"匹配以下模式的字符串:
以 _AB 开始,后跟任意8个非空白字符,以 _ 结束

regex_search和regex_match的区别
std::regex_search
功能:std::regex_search 函数用于搜索字符串中是否存在匹配正则表达式的子串
返回值:如果找到匹配的子串,函数返回 true;如果没有找到匹配的子串,返回 false
参数说明:
s:要搜索的字符串
m:一个 std::smatch 对象,用于存储匹配的结果
re:一个 std::regex 对象,表示要搜索的正则表达式
使用场景:想要查找字符串中是否存在某个模式时,可以使用 std::regex_search
std::regex_match
功能:std::regex_match 函数用于检查整个字符串是否完全匹配正则表达式
返回值:如果字符串完全匹配正则表达式,函数返回 true;如果字符串与正则表达式不匹配,返回 false
参数说明:
s:要检查的字符串
m:一个 std::smatch 对象,用于存储匹配的结果
re:一个 std::regex 对象,表示要匹配的正则表达式
使用场景:想要检查整个字符串是否与某个模式完全匹配时,可以使用 std::regex_match

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值