搜索字符串所有索引&字符串替换&字符串函数处理后替换

python字符串处理常见问题总结,后续待补充。

一,在字符串中搜索一个子字符串的所有索引
  1. 通过字符串的index方法实现
import re
from collections import Iterable, Iterator

sub_str = '5G'
total_str = '''Wang Xiaochu, chairman of China Unicom, said the two companies' 5G co-construction and sharing initiative has helped save them 60 billion yuan ($8.87 billion) in construction investment costs, which enabled them to basically achieve the goal of quickly forming 5G network coverage in the shortest time possible and with the least investment.'''


def all_index_for_str(sub_str, total_str):
    all_index = []
    length1 = len(sub_str)
    i = 0
    while sub_str in total_str[i:]:
        index = total_str.index(sub_str, i)  # 被搜索字符串从该索引位置开始搜索
        all_index.append(index)
        i = index + length1
    return all_index


all_index = all_index_for_str(sub_str, total_str)
print(all_index)

执行结果

[64, 261]
  1. 通过正则表达式实现搜索
def all_index_for_str_by_re(sub_str, total_str):
    all_index = []
    result = re.finditer(sub_str, total_str)
    print(result)  # 迭代器
    print(isinstance(result, Iterable))  # 可迭代对象
    print(isinstance(result, Iterator))  # 迭代器
    for i in result:
        all_index.append(i.span()[0])
    return all_index


all_index = all_index_for_str_by_re(sub_str, total_str)
print(all_index)

执行结果

<callable_iterator object at 0x00000230ABDD7A58>
True
True
[64, 261]
二,在字符串中搜索一个子字符串并用另一个字符串直接替换
  1. 通过字符串的replace方法实现,自定义替换的字符串数量
def replace_str(sub_str, replace_str, total_str, replace_num=None):
    print('*' * 50)
    res = total_str.replace(sub_str, replace_str, replace_num) if replace_num else total_str.replace(sub_str, replace_str)
    return res


print(total_str)
res = replace_str(sub_str, '\n hello', total_str)
print(res)
res = replace_str(sub_str, '\n hello', total_str, 1)
print(res)
  1. 通过正则表达式实现字符串直接替换,自定义替换的字符串数量
def replace_str_by_re(sub_str, replace_str, total_str, replace_num=None):
    print('*' * 50)
    r1 = re.compile(sub_str)
    res = r1.sub(replace_str, total_str, replace_num) if replace_num else r1.sub(replace_str, total_str)
    return res


print(total_str)
res = replace_str_by_re(sub_str, '\n hello', total_str)
print(res)
res = replace_str_by_re(sub_str, '\n hello', total_str, 1)
print(res)

执行结果

Wang Xiaochu, chairman of China Unicom, said the two companies' 5G co-construction and sharing initiative has helped save them 60 billion yuan ($8.87 billion) in construction investment costs, which enabled them to basically achieve the goal of quickly forming 5G network coverage in the shortest time possible and with the least investment.
**************************************************
Wang Xiaochu, chairman of China Unicom, said the two companies' 
 hello co-construction and sharing initiative has helped save them 60 billion yuan ($8.87 billion) in construction investment costs, which enabled them to basically achieve the goal of quickly forming 
 hello network coverage in the shortest time possible and with the least investment.
**************************************************
Wang Xiaochu, chairman of China Unicom, said the two companies' 
 hello co-construction and sharing initiative has helped save them 60 billion yuan ($8.87 billion) in construction investment costs, which enabled them to basically achieve the goal of quickly forming 5G network coverage in the shortest time possible and with the least investment.

三,在字符串中搜索一个子字符串并通过函数处理该字符串后,将返回值进行替换

对字符串中连续的数字进行搜索,对该连续数字求和并使用返回值替换该连续数字。

  1. 使用正则表达式进行替换与函数处理
def replace_str_by_func(r1_tmp, func, total_str, replace_num=None):
    r1 = re.compile(r1_tmp)
    res = r1.sub(func, total_str, replace_num) if replace_num else r1.sub(func, total_str)
    return res


def func(item):
    str1 = item.group()
    res = sum([int(i) for i in str1])
    return str(res)


total_str = 'Wang 123Xiaochu, 23chairman of 56China Unicom'
r1_tmp = r'\d+'
res = replace_str_by_func(r1_tmp, func, total_str)
print(total_str)
print(res)

执行结果

Wang 123Xiaochu, 23chairman of 56China Unicom
Wang 6Xiaochu, 5chairman of 11China Unicom
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值