programming-examples/python/Regular_Expression/Find the occurrence and position of the substrings within a string.py

7 lines
242 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
import re
text = 'Python exercises, PHP exercises, C# exercises'
pattern = 'exercises'
for match in re.finditer(pattern, text):
s = match.start()
e = match.end()
print('Found "%s" at %d:%d' % (text[s:e], s, e))