Python 查找所有匹配的子字符串模式并替换子字符串 [英] Python find all matching substring patterns and replace substring

查看:68
本文介绍了Python 查找所有匹配的子字符串模式并替换子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索一个句子是否有特定的模式.如果没有找到,什么都不做.如果找到模式,则用字符串中的另一个子字符串替换模式.

I want to search if a sentence has particular pattern or not. Do nothing if not found. If pattern found, substitute pattern with another substring in the string.

line1 = "Who acted as `` Bruce Wayne '' in the movie `` Batman Forever '' ?" 
#Desired Result: Who acted as ``Bruce_Wayne'' in the movie ``Batman_Forever'' ? 

#This is what I have tried..    
def findSubString(raw_string, start_marker, end_marker): 

    start = raw_string.index(start_marker) + len(start_marker)
    end = raw_string.index(end_marker, start)
    return raw_string[start:end]

phrase = findSubString(line1, "``", "''")
newPhrase = phrase.strip(' ').replace(' ', '_')
line1 = line1.replace(phrase, newPhrase)

<小时>

当前结果:谁在电影《永远的蝙蝠侠》中扮演了Bruce_Wayne"?

到目前为止,我设法找到了句子中的第一个出现,但没有找到下一个.如何搜索具有匹配模式的所有事件?

So far, I managed to find the first occurrence in the sentence but not the next. How to search for all occurrences with matching pattern?

推荐答案

使用正则表达式:

import re

def findSubString(raw_string, start_marker, end_marker):
    return re.sub(
        r'(?<={}).*?(?={})'.format(re.escape(start_marker), re.escape(end_marker)),
        lambda m: m.group().strip().replace(' ', '_'),
        raw_string)

line1 = "Who acted as `` Bruce Wayne '' in the movie `` Batman Forever '' ?"
line1 = findSubString(line1, "``", "''")
assert line1 == "Who acted as ``Bruce_Wayne'' in the movie ``Batman_Forever'' ?"

没有正则表达式:

def findSubString(raw_string, start_marker, end_marker): 
    result = []
    rest = raw_string
    while True:
        head, sep, tail = rest.partition(start_marker)
        if not sep:
            break
        body, sep, tail = tail.partition(end_marker)
        if not sep:
            break
        result.append(head + start_marker + body.strip().replace(' ', '_') + end_marker)
        rest = tail
    result.append(rest)
    return ''.join(result)

这篇关于Python 查找所有匹配的子字符串模式并替换子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆