【问题标题】:How to split a portion of URL with text and number, into text/number如何将带有文本和数字的 URL 的一部分拆分为文本/数字
【发布时间】:2017-05-11 05:24:52
【问题描述】:

我有这个链接: xyz.forum.com/tech/1823630913/status4906130760727773.html

但我想将其转换为: abc.site.com/status/4906130760727773

请问,我怎样才能做到这一点?

编辑:很抱歉,我没有明确说明我打算在哪里以及如何使用它。 再次抱歉。

但这就是我希望使用它的方式。 好吧,使用 Chrome 插件。 这个 - http://www.requestly.in/rules/ 它允许您提供 URL 和要应用的正则表达式。

请检查一下,让我知道我应该如何继续。

【问题讨论】:

  • 你试过什么?您可以在 / 之间拆分
  • 您使用哪种语言?
  • 给出更多的输入示例。

标签: regex url


【解决方案1】:

尝试此代码并将结果添加到您的网址

 link="xyz.forum.com/tech/1823630913/status4906130760727773.html"
 m=re.search('([a-zA-Z]*)[0-9]*.html',link)
 if m:
     st=m.group(0)
     print m #prints status
 n=re.search('([0-9]*).html',link)
 if n:
     no=n.group(0)
     print no #prints number

【讨论】:

  • 感谢您抽出时间。我已经更新了查询,所以请重新查看。再次感谢!
【解决方案2】:

选择一个正则表达式

(?:https?:\/\/)?xyz\.forum\.com\/tech\/\d+\/status(\d+)\.html

xyz\.forum\.com\/tech\/\d+\/status(\d+)\.html

.*\/status(\d+)\.html?

.*status(\d+)\..*

.*[a-z]+(\d+)\..*

.*(\d+)\..*

.*(\d{16}).*

然后替换为:

abc.site.com/status/$1

或使用您的编程语言获取捕获组 1。
并将其添加到“abc.site.com/status/”

【讨论】:

  • 非常感谢!但这里有一些东西。我已经更新了查询,所以请重新查看。再次感谢!
  • 对不起,安装一些随机的未知插件只是为了解决某人的问题是不值得的。有很大的潜在安全风险。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2012-12-30
  • 1970-01-01
  • 2017-04-09
  • 2019-12-01
  • 2015-11-20
  • 2015-08-07
相关资源
最近更新 更多