Regex in python doesn't match anything -


i have piece of code testing regular expressions in python.

#!/usr/bin/python import re  line = "(400,71.20,73.40) cats smarter dogs"  matchobj = re.match( r'(\d{3}),(\d{2}.\d{2}),(\d{2}.\d{2})', line,re.i)  if matchobj:    print "matchobj.group() : ", matchobj.group()    print "matchobj.group(1) : ", matchobj.group(1)    print "matchobj.group(2) : ", matchobj.group(2) else:    print "no match!!" 

it's supposed print 3 groups (400)(71.20)(73.40), instead prints "no match!!".

can me?

thanks in advance

it's because of function re.match tries match start of string. since there unmatched ( present @ start, regex fails. adding pattern match starting ( symbol in re.match solves problem.

re.match( r'\((\d{3}),(\d{2}\.\d{2}),(\d{2}\.\d{2})', line) 

and re.i unneceassry here since matching chars other alphabets.

or

i suggest use re.search match substring exists anywhere.

re.search( r'(\d{3}),(\d{2}\.\d{2}),(\d{2}\.\d{2})', line) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -