Kent Johnson wrote: > cedric briner wrote: >> To let you know, I'm writing a script to generate bind9 configuration >> from a nis hosts table. So I was trying in a one re to catch from this: >> >> <ip> <hostname> [ <cname> ...] [# comment] >> e.g: >> 10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment >> 37.64.86.23 hostname2 >> 35.25.89.34 hostname3 alias5 >> .. .. >> >> >> so I was wishing to write an re expresion which will do in a one step >> all the job ! maybe am'I expecting too much from re :) > > You can use an re to get the four primary fields, then use split() to > break up the aliases. Try this: > > import re > data = '''10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice > comment > 37.64.86.23 hostname2 > 35.25.89.34 hostname3 alias5 > '''.splitlines() > > for line in data: > match = re.search(r'^(\S+)\s+(\S+)([^#]*)(#.*)?$', line) > if match: > ip, host, aliases, comment = match.group(1, 2, 3, 4) > print 'ip:', ip > print 'host:', host > if aliases: > aliases = aliases.strip().split() > for alias in aliases: > print 'alias:', alias > if comment: > print comment > print I had done that before writing my first email. I knew how to do this in few python steps. But some how I was expecting RE beeing able to do this in an one shoot.
Thanks for your precious help. Ced. > > Kent > -- Cedric BRINER Geneva - Switzerland _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor