使用Python进行关键字搜索

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

我想使用Python在字符串中搜索多个关键字 例如我有一个字符串:

INPUT STRING ="无法输入信用卡付款。请通过“[email protected]”或我的其他 ID“[email protected]”回复我。也可以拨打我的电话号码 967544367 来联系我。

我想搜索字符串是否包含:

模式(或关键字),例如“@gmail”、“@yahoo”或如果包含数字[0-9]。然后将整个单词替换为“XXXX”。

在 SQL 中,我可以使用 LIKE OPERATOR 作为单词“%@gmail.com”以及类似的数字映射。 但我想使用 Python 做类似的事情。

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

OUTPUT STRING ="无法输入信用卡付款。请通过 XXXX 或我的其他 ID XXXX 回复我。也可以拨打我的电话号码 XXXX .

python regex encoding pattern-matching extract
1个回答
0
投票

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

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.search("[a-z0-9]*@([\w-]+\.)[a-z]*", txt)
basicNumbersRegex = re.search("[0-9]*", txt)

查看https://regex101.com/

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