regular expression with group to a string including ( ) and dash
Question:
I am trying to use regular expression to find number. But I never succeed. Can anyone help me with it?
import re
item='800-850(0.2)'
find_list=re.findall(r'(d+)-(d+)((d+))$',item)
print(find_list)
[]
The above return empty [], what I wanted is [800,850,0.2]. I am using () to capture group. do I use ( and ) to indicate ( or ) ? and use – to indicate –
Thanks for your help.
Answers:
You also need to accept the dot (.
) for the last number, which is not included by d
(equivalent to [0-9]
).
re.findall(r'(d+)-(d+)(([d.]+))$',item)
To make sure the dot is between two consecutive groups of digits, use (d+.d+)
.
I am trying to use regular expression to find number. But I never succeed. Can anyone help me with it?
import re
item='800-850(0.2)'
find_list=re.findall(r'(d+)-(d+)((d+))$',item)
print(find_list)
[]
The above return empty [], what I wanted is [800,850,0.2]. I am using () to capture group. do I use ( and ) to indicate ( or ) ? and use – to indicate –
Thanks for your help.
You also need to accept the dot (.
) for the last number, which is not included by d
(equivalent to [0-9]
).
re.findall(r'(d+)-(d+)(([d.]+))$',item)
To make sure the dot is between two consecutive groups of digits, use (d+.d+)
.