I am quite new to regex and I would like to know if there was a way to match something that is either a normal character : \w+
or a question mark. I am trying to retrieve the value after the n
.
The line has this format :
n : ?, t : 0.4
The thing that follows n can also be an integer :
n : 3, t : 0.7
So far I have :
import re
line = "n : 4, t : 0.4"
value = re.findall(r'\w : (\w+), \w : \d\.\d+', line)
How can I change this to take into account the fact that line
can contain a question mark ?
Expected output :
- if following
n
is a ?, value should be None - if following
n
is an integer, value should be that integer