How to checks for a match at the beginning of a string using regex in Python

2 Answers

0 votes
import re

m = re.match("ab", "abcdefg") 

if m:
  print('found:', m.group())
else:
  print('not find')


 
 
   
'''
run:
   
found: ab
   
'''

 



answered Aug 1, 2019 by avibootz
0 votes
import re

s = re.search("^ab", "abcdefg") 

if s:
  print('found:', s.group())
else:
  print('not find')


 
 
   
'''
run:
   
found: ab
   
'''

 



answered Aug 1, 2019 by avibootz
...