Reputation: 97
There is a sentence "i have 5 kg apples and 6 kg pears".
I just want to extract the weight of apples.
So I use
sentence = "I have 5 kg apples and 6 kg pears"
number = re.findall(r'(\d+) kg apples', sentence)
print (number)
However, it just works for integer numbers. So what should I do if the number I want to extract is 5.5?
Upvotes: 4
Views: 1310
Reputation: 8043
The regex you need should look like this:
(\d+.?\d*) kg apples
You can do as follows:
number = re.findall(r'(\d+.?\d*) kg apples', sentence)
Here is an online example
Upvotes: 0
Reputation: 17054
You can try something like this:
import re
sentence = ["I have 5.5 kg apples and 6 kg pears",
"I have 5 kg apples and 6 kg pears"]
for sen in sentence:
print re.findall(r'(\d+(?:\.\d+)?) kg apples', sen)
Output:
['5.5']
['5']
Upvotes: 2
Reputation: 8003
Non-regex solution
sentence = "I have 5.5 kg apples and 6 kg pears"
words = sentence.split(" ")
[words[idx-1] for idx, word in enumerate(words) if word == "kg"]
# => ['5.5', '6']
You can then check whether these are valid floats using
try:
float(element)
except ValueError:
print "Not a float"
Upvotes: 1
Reputation: 95948
You change your regex to match it:
(\d+(?:\.\d+)?)
\.\d+
matches a dot followed by at least one digit. I made it optional, because you still want one digit.
Upvotes: 0
Reputation: 737
You can use number = re.findall(r'(\d+\.?\d*) kg apples', sentence)
Upvotes: 0
Reputation: 50177
?
designates an optional segment of a regex.
re.findall(r'((\d+\.)?\d+)', sentence)
Upvotes: 0