regex - Python re.search - searching from left to right -


i've got small problem. i've written module parse configuration file (wvdial's wvdial.conf) using regex. file contains strings "init1 = at" , i've used following regex:

match = re.match(r'(.*)=(.*)', line) 

everything worked until following line:

#init3 = at+cpin="0000" 

which got parsed like:

'#init3 = at+cpin':'0000' 

it seems regex engine goes right left parsing string. there way reverse re.search direction?

you need mark first * quantifier non-greedy appending ?:

match = re.match(r'(.*?)=(.*)', line) 

demo:

>>> line = '#init3 = at+cpin="0000"' >>> re.match(r'(.*?)=(.*)', line).group() '#init3 = at+cpin="0000"' 

by making quantifier non-greedy, regular expression engine match minimum satisfy pattern, rather maximum.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -