Python文本处理

约束搜索

约束搜索详细操作教程
很多时候,在得到搜索结果之后,我们需要更深入地搜索现有搜索结果的一部分。 例如,在给定的文本主体中,我们的目标是获取Web地址,并提取Web地址的不同部分,如协议,域名等。在这种情况下,需要借助用于划分的组功能 搜索结果以各个组为基础,分配正则表达式。 我们通过使用可搜索部分周围的括号分隔主搜索结果来创建这样的组表达式,不包括想要匹配的固定单词。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
import re
text = "The web address is https://www.lidihuo.com"
# Taking "://" and "." to separate the groups
result = re.search('([\w.-]+)://([\w.-]+)\.([\w.-]+)', text)
if result :
    print "The main web Address: ",result.group()
    print "The protocol: ",result.group(1)
    print "The doman name: ",result.group(2)
    print "The TLD: ",result.group(3)
执行上面的示例代码,得到以下结果 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
The main web Address: https://www.lidihuo.com
The protocol: https
The doman name: www.lidihuo
The TLD: com
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4