首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在正则表达式匹配后使用正则表达式删除文本的特定部分

如何在正则表达式匹配后使用正则表达式删除文本的特定部分
EN

Stack Overflow用户
提问于 2019-10-04 05:35:46
回答 1查看 421关注 0票数 0

(不,Python regex, how to delete all matches from a string不能解决我的问题)

假设我有这个列表:

代码语言:javascript
运行
复制
names = ['your name', 'the name', 'his name', 'her name', 'their name', 'employer name', "employer's name", "father's name",
        "mother's name", "maiden name", "son's name", "daughter's name", "brother's name", "sister's name"]

假设我有这样一段文字:

代码语言:javascript
运行
复制
text = "What is your name?  Well,  uh it's John Smith.  Thanks for asking. Anyway, I'd doing well."

如何使用regex查找文本中列表名称的每个元素,并将紧跟在元素后面的文本块(长度为50)替换为“name”。因此,我的输出将是:

代码语言:javascript
运行
复制
text = "What is your name [name] Anyway, I'd doing well."

到目前为止,我有下面的代码,但它只用“name”替换了元素,而不是元素后面的实际文本。

代码语言:javascript
运行
复制
def my_replace3(match):
    match = match.group()
    return " [name] "

def no_name(text):
    names = ['your name', 'the name', 'his name', 'her name', 'their name', 'employer name', "employer's name", "father's name",
        "mother's name", "maiden name", "son's name", "daughter's name", "brother's name", "sister's name"]
    regex = re.compile(r'\b(' + '|'.join(names) + r')\b', re.IGNORECASE)
    text = re.sub(regex, my_replace3, text)
    return text

我不是一个伟大的正则表达式专家,所以您的帮助将非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-04 05:42:17

如果要在匹配后替换50个字符,请将.{50}添加到正则表达式中。

然后在替换字符串中使用反向引用将匹配的字符串复制到替换字符串。

代码语言:javascript
运行
复制
def no_name(text):
    names = ['your name', 'the name', 'his name', 'her name', 'their name', 'employer name', "employer's name", "father's name",
        "mother's name", "maiden name", "son's name", "daughter's name", "brother's name", "sister's name"]
    regex = re.compile(r'\b(' + '|'.join(map(re.escape, names)) + r')\b.{50}', re.IGNORECASE)
    text = re.sub(regex, r'\1 [name]', text)
    return text

在插入应该与正则表达式完全匹配的字符串时,也应该使用re.escape(),以防其中任何字符串包含正则表达式运算符。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58227193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档