Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53223

Re: String splitting with exceptions

Newsgroups comp.lang.python
Date 2013-08-29 00:26 -0700
References <kvl9e5$19gk$1@leila.iecc.com>
Message-ID <f1014141-3977-49b6-86ac-2de9e72ee3f0@googlegroups.com> (permalink)
Subject Re: String splitting with exceptions
From wxjmfauth@gmail.com

Show all headers | View raw


Le mercredi 28 août 2013 18:44:53 UTC+2, John Levine a écrit :
> I have a crufty old DNS provisioning system that I'm rewriting and I
> 
> hope improving in python.  (It's based on tinydns if you know what
> 
> that is.)
> 
> 
> 
> The record formats are, in the worst case, like this:
> 
> 
> 
> foo.[DOM]::[IP6::4361:6368:6574]:600::
> 
> 
> 
> What I would like to do is to split this string into a list like this:
> 
> 
> 
> [ 'foo.[DOM]','','[IP6::4361:6368:6574]','600','' ]
> 
> 
> 
> Colons are separators except when they're inside square brackets.  I
> 
> have been messing around with re.split() and re.findall() and haven't
> 
> been able to come up with either a working separator pattern for
> 
> split() or a working field pattern for findall().  I came pretty
> 
> close with findall() but can't get it to reliably match the
> 
> nothing between two adjacent colons not inside brackets.
> 
> 
> 
> Any suggestions? I realize I could do it in a loop where I pick stuff
> 
> off the front of the string, but yuck.
> 
> 
> 
> This is in python 2.7.5.
> 
> 
> 
> -- 
> 
> Regards,
> 
> John Levine, johnl@iecc.com, Primary Perpetrator of "The Internet for Dummies",
> 
> Please consider the environment before reading this e-mail. http://jl.ly

----------

Basic idea: protect -> split -> unprotect

>>> s = 'foo.[DOM]::[IP6::4361:6368:6574]:600::'
>>> r = s.replace('[IP6::', '***')
>>> a = r.split('::')
>>> a
['foo.[DOM]', '***4361:6368:6574]:600', '']
>>> a[1] = a[1].replace('***', '[IP6::')
>>> a
['foo.[DOM]', '[IP6::4361:6368:6574]:600', '']

jmf

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

String splitting with exceptions John Levine <johnl@iecc.com> - 2013-08-28 16:44 +0000
  Re: String splitting with exceptions Skip Montanaro <skip@pobox.com> - 2013-08-28 11:55 -0500
  Re: String splitting with exceptions random832@fastmail.us - 2013-08-28 13:14 -0400
    Re: String splitting with exceptions John Levine <johnl@iecc.com> - 2013-08-28 21:35 +0000
  Re: String splitting with exceptions Tim Chase <python.list@tim.thechases.com> - 2013-08-28 12:32 -0500
    Re: String splitting with exceptions Neil Cerutti <neilc@norwich.edu> - 2013-08-28 18:18 +0000
  Re: String splitting with exceptions Neil Cerutti <neilc@norwich.edu> - 2013-08-28 18:08 +0000
    Re: String splitting with exceptions Peter Otten <__peter__@web.de> - 2013-08-28 20:31 +0200
  Re: String splitting with exceptions wxjmfauth@gmail.com - 2013-08-29 00:26 -0700

csiph-web