Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract number followed and preceded by the words

Extract numbers followed and preceded by words:

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)

it gives the list of all words and numbers in the gives q. so now i want method to get number along with words

like image 720
Rahul Ranjan Avatar asked Nov 12 '22 03:11

Rahul Ranjan


1 Answers

Based on your description I guess you need something like this:

>>> 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']
like image 175
Ashwini Chaudhary Avatar answered Nov 15 '22 11:11

Ashwini Chaudhary