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 ?
5 Answers
+ 7
import re
s = 'halfbright best brave broken'
result = re.findall(r'\bbr..e.\b', s)
print(result)
+ 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
+ 3
Mndm
could you please enumerate some other values of p?
+ 1
Jan Markus
Input : p = br??e?
s = bright best brave broken
Output : broken
0
Jan Markus those are just sample inputs. What if I give some other value for 'p' ?