Regexp: unexspected splitting of string in several groups

Robert Brewer fumanchu at amor.org
Mon May 31 09:30:24 EDT 2004


Piet wrote:
> vartype(width[,decimals]|list) further variable attributes.
> Typical examples are:
> char(30) binary
> int(10) zerofill
> float(3,2)...
> I would like to extract the vartype, the bracketed string and the
> further properties separately and thus defined the following regular
> expression:
> #snip
> vartypePattern = re.compile("([a-zA-Z]+)(\(.*\))*([^(].*[^)])")
> vartypeSplit = vartypePattern.match("float(3,2) not null")

You might try collecting the parentheses and "further attributes" into
their own group:

>>> a = "char(30) binary"
>>> b = "float"
>>> pat = r"([a-zA-Z]+)((\(.*\))(.*))*"
>>> re.match(pat, a).groups()
('char', '(30) binary', '(30)', ' binary')
>>> re.match(pat, b).groups()
('float', None, None, None)


Hope that helps,

Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list