【问题标题】:how to use a regular expression to achieve the following output?如何使用正则表达式来实现以下输出?
【发布时间】:2017-01-11 18:43:32
【问题描述】:

我正在清理我尝试过的网址中的数据:

s = 'hello http://www.google.com I am william http://www.google.com'

from urlparse import urlparse

s.split()

clean = ' '.join([el for el in [i for i in s.split()] if not urlparse(el).scheme])

print(clean)

想要的输出:

hello I am william

但是这次我想使用 而是一个正则表达式。

【问题讨论】:

  • 这是一个尴尬的问题。请参阅 mathiasbynens.be/demo/url-regex 了解一些完美 url 正则表达式的尝试。如果你知道你的 url 总是有某种格式,这个问题就会大大简化。
  • 检查herehere以及here
  • regex101.com 是一个不错的在线、python 风格的正则表达式测试器。

标签: python regex


【解决方案1】:

使用替换

import re

s = 'hello http://www.google.com I am william http://www.google.com'
print(re.sub('http\S+\s?', '', s))

打印

hello I am william

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多