Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created August 14, 2020 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/195e178ce90df0db15f8bd6acfb5236e to your computer and use it in GitHub Desktop.
Save BetterProgramming/195e178ce90df0db15f8bd6acfb5236e to your computer and use it in GitHub Desktop.
import re
string = 'banana'
pattern = 'an'
match=(re.search(pattern, string))
#Returns match object
print (match)#Output:<re.Match object; span=(1, 3), match='an'>
#getting the starting index using match.start()
print (match.start()) #Output: 1
#Getting the start and end index in tuple format using match.span()
print (match.span()) #Output: (1,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment