【问题标题】:Extract number followed and preceded by the words提取数字后面和前面的单词
【发布时间】:2013-06-17 20:28:46
【问题描述】:

提取单词前后的数字:

String q = 'Consumer spending in the US rose to about 62% of GDP in 1960, where it stayed until about 1981, and has since risen to 71% in 2013'
q = re.findall(r'^([^\d]+)\s(\d+)\s*,\s*([^\d]+)\s(\d+)',s)

它给出了q 中所有单词和数字的列表。 所以现在我想要方法来获取数字和单词

【问题讨论】:

  • 你期望的输出是什么?
  • 当它看起来像Java 时,为什么它被标记为python

标签: python html beautifulsoup


【解决方案1】:

根据你的描述,我猜你需要这样的东西:

>>> import re
>>> strs = 'Consumer spending in the US rose to about 62% of GDP in 1960, where it stayed until about 1981, and has since risen to 71% in 2013'
>>> re.findall(r'\w+\s\d+.*?\s\w+',strs)
['about 62% of', 'in 1960, where', 'about 1981, and', 'to 71% in']

【讨论】:

  • @RahulRanjan 这是你想要的还是我应该删除解决方案?
猜你喜欢
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 2018-05-24
  • 2011-04-02
  • 2020-06-17
  • 2020-02-21
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多