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
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With