python - use RE to find string consisting only of Latin letters, digits and underscores and they can't start with a digit -


i want use regular expression('re') find if variable names consist of latin letters, digits , underscores , can't start digit.

i tried using

in [3]: name='qq-q'  in [4]: re.match("[a-za-z_][0-9a-za-z_]*",name) out[4]: <_sre.sre_match object; span=(0, 2), match='qq'>  in [5]: name='kri[shna0'  in [6]: re.match("[a-za-z_][0-9a-za-z_]*",name) out[6]: <_sre.sre_match object; span=(0, 3), match='kri'> 

can 1 explain me why above expression matches '-' , '[' in above?

you there! in regex, * matches 0 or more of given character, matching longest sequence. instance a* match aaabcde , match aaa. match bcde wit empty match, match nonetheless. achieve want need add $ @ end of pattern:

re.match("[a-za-z_][0-9a-za-z_]*$",name) 

this requests pattern match input until end of line, represented $

if using re.search, need start pattern ^. not necessary re.match since matches beginning of string: python3 doc: search vs match


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -