sscanf ?

Bruce Edge bedge at troikanetworks.com
Tue Nov 20 16:22:20 EST 2001


In <3BFAC45B.9D246137 at alcyone.com>, Erik Max Francis wrote:

> Bruce Edge wrote:
> 
>> Additionally, after parsing with re, I still need to convert to
>> integers,
>> which is a PITA when you consider the "%x" case. The format string
>> "knows"
>> that this is in hex, using re, I get back say a string which I still
>> need
>> to dtermine the number base and convert.
> 
> You can use the int-with-arg form for converting strings to integers
> other than in base ten.
> 

Ahh, OK, yes, that does it. Thanks.

>> I'm not arguing :)
>> It's just that in this one tiny case, scanf would have been the
>> perfect
>> tool.
> 
> It wouldn't be hard to write a simple wrapping script that takes a
> printf format string, converts it to a regular expression, does a match
> against a string, then pulls out the arguments and converts them to
> types as appropriate.  But one might just as well be using regular
> expressions directly in the first place.
> 

Here's what I did to convert the format string to a regex.
It's not pretty, and many types will break it, but it serves the
purpose, for now:

def fmtstr2regex( str ):
	regex = ""
	while len(str):
		if str[0] == '%':
			x = re.match( "%(?P<len>\d*)(?P<type>\w)(?P<rest>.*)$", str )
			if not x:
				exc = CommandException()
				exc.reason = "Invalid print format specifier %s" % str
				raise exc
			length = int( x.group("len") )
			type = x.group("type")
			# Some types need to be changed from printf to regexp world
			if type == 'x':
				type = '['+string.hexdigits+']'
			else:
				type = "\\%s" % type
			regex += "(%s" % type
			if length:
				regex += "{%d,%d})" % ( length, length )
			else:
				regex += "+)"
			str = x.group('rest')
		else:
			regex += "\%s" % str[0]
			str = str[1:]
	return regex



More information about the Python-list mailing list