Extracting number from string in Python with regex

python, regex

Solution

Non-regex solution is:

myString = "Test1 [cm]:     -35.00/-34.99/-34.00/0.09"  
print myString.split("/")[1]

Test this code here.

One of regex solutions would be:

import re 
myString = "Test1 [cm]:     -35.00/-34.99/-34.00/0.09" 
print re.search(r'(?<=\/)[+-]?\d+(?:\.\d+)?', myString).group()

Test this code here.

Explanation:

(?<=\/)[+-]?\d+(?:\.\d+)?
└──┬──┘└─┬─┘└┬┘└───┬────┘
   │     │   │     │
   │     │   │     └ optional period with one or more trailing digits
   │     │   │
   │     │   └ one or more digits
   │     │
   │     └ optional + or -
   │
   └ leading slash before match 

Problem

I want to extract and print a variable number '-34.99' from the string: ``` myString = "Test1 [cm]: -35.00/-34.99/-34.00/0.09" ``` The values in the string will change. How can I do it with the regular expression in Python? Thanks in advance

Original source