使用Python进行关键字搜索和替换[关闭]

问题描述 投票:0回答:1

我想使用 Python 搜索和替换字符串中的多个关键字(例如模式)。

例如我有一个字符串:

INPUT_STRING = "Can not enter credit card payment. Please reply me at "[email protected]" or my other id "[email protected]". Also call me at my phone number 967544367 .

我想搜索字符串是否包含“@gmail”、“@yahoo”等模式(或关键字),或者是否包含数字 [0-9]。之后我想用“XXXX”替换匹配项。

在 SQL 中,我可以使用 LIKE 运算符作为像

%@gmail.com
这样的词以及类似的数字映射。但我想使用 Python 做类似的事情。

下面是我正在寻找的输出:

OUTPUT_STRING = "Can not enter credit card payment. Please reply me at XXXX or my other id XXXX. Also call me at my phone number XXXX."
python regex
1个回答
1
投票

您可以使用正则表达式包来实现此目的:

import re

txt = 'Can not enter credit card payment. Please reply me at "[email protected]" or 
my other id "[email protected]". Also call me at my phone number 967544367 .'

basicEmailRegex = re.findall("[a-z0-9]*@[\w-]+\.[a-z]*", txt)
basicNumbersRegex = re.search("[0-9]+", txt)

更换方法如下:

basicEmailRegex = re.sub("[a-z0-9]*@[\w-]+\.[a-z]*","XXXX", txt )
basicEmailRegex = re.sub("[0-9]+","XXXX", basicEmailRegex )

结果:

'Can not enter credit card payment. Please reply me at "XXXX" or my other id "XXXX". Also call me at my phone number XXXX .'

查看https://regex101.com/

请注意,电子邮件正则表达式可能无法捕获所有可能的有效电子邮件地址,但可以让您了解如何继续。您可以搜索解释电子邮件验证正则表达式的不同问题。

© www.soinside.com 2019 - 2024. All rights reserved.