Reputation: 35
I have the following string:
schema(field1, field2, field3, field4 ... fieldn)
I need to transform the string to an object with name attribute as schema
and the field names as another attribute which is a list.
How do I do this in Python with a regular expression?
Upvotes: 1
Views: 336
Reputation: 4085
You could use something like (in two rounds because python re doesn't support nested capture (thanks SilentGhost for pointing it out)) :
pattern = re.compile("^([a-z]+)\(([a-z,]*)\)$")
ret = pattern.match(s)
if ret==None:
...
else:
f = ret.groups()
name = f[0]
args = f[1]
arg_pattern = re.compile("^([a-z]+)(,[a-z]+)*$")
ret2 = arg_pattern.match(args)
# same checking as above
if (ret2==None):
...
else:
args_f = ret2.groups()
Upvotes: 0
Reputation: 12601
Regular expressions for things like that probably need tests:
import unittest
import re
# Verbose regular expression! http://docs.python.org/library/re.html#re.X
p = r"""
(?P<name>[^(]+) # Match the pre-open-paren name.
\( # Open paren
(?P<fields> # Comma-separated fields
(?:
[a-zA-Z0-9_-]+
(?:,\ ) # Subsequent fields must separated by space and comma
)*
[a-zA-Z0-9_-]+ # At least one field. No trailing comma or space allowed.
)
\) # Close-paren
"""
# Compiled for speed!
cp = re.compile(p, re.VERBOSE)
class Foo(object):
pass
def validateAndBuild(s):
"""Validate a string and return a built object.
"""
match = cp.search(s)
if match is None:
raise ValueError('Bad schema: %s' % s)
schema = match.groupdict()
foo = Foo()
foo.name = schema['name']
foo.fields = schema['fields'].split(', ')
return foo
class ValidationTest(unittest.TestCase):
def testValidString(self):
s = "schema(field1, field2, field3, field4, fieldn)"
obj = validateAndBuild(s)
self.assertEqual(obj.name, 'schema')
self.assertEqual(obj.fields, ['field1', 'field2', 'field3', 'field4', 'fieldn'])
invalid = [
'schema field1 field2',
'schema(field1',
'schema(field1 field2)',
]
def testInvalidString(self):
for s in self.invalid:
self.assertRaises(ValueError, validateAndBuild, s)
if __name__ == '__main__':
unittest.main()
Upvotes: 1
Reputation: 320019
Are you looking for something like this?
>>> s = 'schema(field1, field2, field3, field4, field5)'
>>> name, _, fields = s[:-1].partition('(')
>>> fields = fields.split(', ')
>>> if not all(re.match(r'[a-z]+\d+$', i) for i in fields):
print('bad input')
>>> sch = type(name, (object,), {'attr': fields})
>>> sch
<class '__main__.schema'>
>>> sch.attr
['field1', 'field2', 'field3', 'field4', 'field5']
Upvotes: 5