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

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

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
最新回复(0)
/jishu82bK7nVusSkTE3ERoIVPfNI2TRPejvMPUK1z1Q_3D_3D8