Arya Voronova
Arya Voronova

Reputation: 527

Python re.search - searching from left to right

I've got a small problem. I've written a module to parse a configuration file (wvdial's wvdial.conf) using regex. File contains strings like "Init1 = AT" and I've used a 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 like the regex engine goes from right to left parsing the string. Is there any way to reverse the re.search direction?

Upvotes: 0

Views: 2419

Answers (3)

Hai Vu
Hai Vu

Reputation: 40733

I believe you are using the wrong tool. It seems wvdial.conf is really an .ini file format. Python comes with the ConfigParser module, which can easily handle it:

import ConfigParser

# Sample wvdial.conf
'''
[Dialer Defaults] 
Modem = /dev/ttyS2 
Baud = 57600 

[Dialer shh] 
Init3 = ATM0

[Dialer pulse] 
Dial Command = ATDP
'''

configuration = ConfigParser.SafeConfigParser()
configuration.read('wvdial.conf')
init3 = configuration.get('Dialer shh', 'Init3') # ATM0
print init3

Upvotes: 0

Steve Barnes
Steve Barnes

Reputation: 28380

No python re doesn't parse from right to left but .* is by default greedy it will look for the longest match it can get, you can get your regex to work in a couple of ways easiest is:

re.match(r'([^=]+)=(.*)', line)

This will match anything but = as the first part of the match then = then anything after the first =

Demo:

>>> import re
>>> line = '#Init3 = AT+CPIN="0000"'
>>> re.match(r'([^=]+)=(.*)', line).groups()
('#Init3 ', ' AT+CPIN="0000"')
>>> 

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1122092

You need to mark your first * quantifier as non-greedy by appending ?:

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

Demo:

>>> line = '#Init3 = AT+CPIN="0000"'
>>> re.match(r'(.*?)=(.*)', line).group()
'#Init3 = AT+CPIN="0000"'

By making the quantifier non-greedy, the regular expression engine will match the minimum to satisfy the pattern, rather than the maximum.

Upvotes: 2

Related Questions