+ 2

Python regex match question

Input exactly as below:: List of devices attached emulator-5554 device Need to perform regex match to get the value before (\tdevice) that is emulator-5554 I was trying below code: m = re.findall(r'(\tdevice)',a)[0] if m: print m else: print "none"

5th Sep 2017, 6:56 PM
Saleem Hussain
Saleem Hussain - avatar
3 Antworten
+ 6
That will work too, but the search will never return None this way. It will always return a value, even if it's blank.
5th Sep 2017, 7:27 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Check it out, just input: device https://code.sololearn.com/c53M1pnKpmvY/?ref=app Try out your regexes at: www.regex101.com
5th Sep 2017, 7:18 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
m = re.findall("(.+)\tdevice",a) if m: print m else: print "none"
5th Sep 2017, 7:24 PM
Saleem Hussain
Saleem Hussain - avatar