【问题标题】:how to extract these strings in Python using regex如何使用正则表达式在 Python 中提取这些字符串
【发布时间】:2022-10-13 17:29:02
【问题描述】:

我在下面有一个字符串:

stra = "hello (70/2009). Target no. 39/K/20/MEM/2019 world no. 12/2020 good 21/PMK.011/2010 test"

我想提取以下包含“/”符号的子字符串:

  1. 70/2009
  2. 39/K/20/MEM/2019
  3. 2020 年 12 月
  4. 21/PMK.011/2010

    谁能建议我如何做到这一点?

    非常感谢!

【问题讨论】:

  • \S+/\S+ 之类的东西应该可以工作
  • \d+\/\d+ 怎么样

标签: python regex


【解决方案1】:

不是正则表达式,但您可以像这样拆分然后在子字符串中找到“/”。

stra = "hello (70/2009). Target no. 39/K/20/MEM/2019 world no. 12/2020 good 21/PMK.011/2010 test"
l = [x for x in stra.split() if '/' in x]
print(l)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多