programming-examples/python/Regular_Expression/Search a literals string in a string and also find the location where the pattern occurs.py
2019-11-15 12:59:38 +01:00

8 lines
259 B
Python

import re
pattern = 'fox'
text = 'The quick brown fox jumps over the lazy dog.'
match = re.search(pattern, text)
s = match.start()
e = match.end()
print('Found "%s" in "%s" from %d to %d ' % \
(match.re.pattern, match.string, s, e))