regex - 只输出匹配的正则表达式模式
问题描述
我有一个包含 10,000 行的 csv 文件。每行有 8 列。其中一列包含与此类似的文本:
this is a row: http://somedomain.com | some_text | http://someanotherdomain.com | some_more_text
this is a row: http://yetanotherdomain.net
this is a row: https://hereisadomain.org | some_text
我目前正在以这种方式访问此列中的数据:
for row in csv_reader:
the_url = row[3]
# this regex is used to find the hrefs
href_regex = re.findall('(?:http|ftp)s?://.*', the_url)
for link in href_regex:
print (link)
打印语句的输出:
http://somedomain.com | some_text | http://someanotherdomain.com | some_more_text
http://yetanotherdomain.net
https://hereisadomain.org | some_text
如何仅获取 URL?
http://somedomain.com
http://someanotherdomain.com
http://yetanotherdomain.net
https://hereisadomain.org
解决方案
推荐阅读
- javascript - 使用自定义数据和布局属性以编程方式创建图标
- react-native - ReactNative 问题与构造函数说';' 错误
- java - 使用 Rest Assured 和 TestNG 处理异常/失败
- three.js - 相交网格结果与整体网格
- python - 我在最后一行没有输入任何参数,但是我收到了这个错误:“TypeError: draw() 接受 0 个位置参数,但给出了 1 个”
- php - 如何按照订单创建 PHP 页面?
- javascript - 做出反应。在收到服务器的肯定响应之前,如何发送请求?
- r - 返回数据框中包含 R 中“x”元素的行
- blazor - Blazor - 检测任何表单值何时更改
- python - 在 Python 中计算浮点数数组的 crc32 校验和