javascript - 如何匹配 URL 但不是模式
问题描述
我需要 js 正则表达式来提取字符串中的 url,但不需要像这样的模式:[[url=http://example.com/foo]]
.
示例:这是一个测试字符串http://google.com
, https://www.youtube.com/watch?v=K_m7NEDMrV0
, https://instagram.com/hellow/
ftp://google.com
[[url=https://instagram.com/hellow/]]
。
正则表达式应该提取:
http://google.com
https://www.youtube.com/watch?v=K_m7NEDMrV0&test=1
https://instagram.com/hellow/
解决方案
我不确定你的意思,这是适合你的解决方案吗?
import re
# input strings
url_strings = ['http://google.com',
'https://www.youtube.com/watch?v=K_m7NEDMrV0',
'https://instagram.com/hellow/',
'ftp://google.com [[url=https://instagram.com/hellow/]]']
for i in range(len(url_strings)):
r1 = re.findall(r'\[', str(url_strings[i]))
if not r1:
print(url_strings[i])
推荐阅读
- typescript - 如何在 next.js 中使用 Algolia 地方?
- php - PhpDoc 注释:如何在一个地方“指向”一个变量
- javascript - 以像素为单位设置交叉点观察者的阈值?或类似的东西?
- ios - 在 CmdLine@2 任务中设置 Azure DevOps 管道中的 Xcode 版本
- javascript - 任务 ':app:mergeDebugAssets' 执行失败。....要求:项目:app > 项目:react-native-track-player
- java - 如何在 Selenium 迭代期间访问每个元素的非唯一子类?
- ios - 当我点击时如何启用。iOS 中的缺口进入屏幕顶部
- rebus - 请求-回复更多客户端和一台服务器
- wpf - Helix 工具包,隐藏或缩小原点(枢轴)
- python - 这个问题的通用修复方法是什么——“RuntimeWarning:模块 ABC 的编译时版本 X 与运行时版本 Y 不匹配”?