0

Is is possible to do this with REGEX ?

The program must find the first occurrence of the word in string 's' that matches the pattern 'p' Input : p = ?i?n s = Crime lion keen Output : lion I can do the other way but is it possible to solve using regular exp ?

14th Oct 2020, 4:01 AM
Levi
Levi - avatar
5 odpowiedzi
+ 7
import re s = 'halfbright best brave broken' result = re.findall(r'\bbr..e.\b', s) print(result)
14th Oct 2020, 4:56 AM
Jan Markus
+ 6
import re s = 'Crime lion keen' result = re.search(r'\b.i.n\b', s).group(0) print(result) Explanation: \b = word-border . = single arbitrary character
14th Oct 2020, 4:20 AM
Jan Markus
+ 3
Mndm could you please enumerate some other values of p?
14th Oct 2020, 4:36 AM
Jan Markus
+ 1
Jan Markus Input : p = br??e? s = bright best brave broken Output : broken
14th Oct 2020, 4:39 AM
Levi
Levi - avatar
0
Jan Markus those are just sample inputs. What if I give some other value for 'p' ?
14th Oct 2020, 4:29 AM
Levi
Levi - avatar