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


Groups > comp.lang.python > #75860

Re: more simple to split the string?

From Christopher Welborn <cjwelborn@live.com>
Subject Re: more simple to split the string?
Date 2014-08-07 20:29 -0500
References <53E4186A.5020201@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12732.1407461393.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 08/07/2014 07:23 PM, elearn wrote:
> str='(\\HasNoChildren \\Junk) "/" "[Gmail]/&V4NXPpCuTvY-"'
> x=str.split(' "')
> [i.replace('"','') for i in x]
> ['(\\HasNoChildren \\Junk)', '/', '[Gmail]/&V4NXPpCuTvY-']
>
> x.strip(" ") will create four parts.
>
> is there more simple to do that ?

There are many different ways to do this. I don't usually
recommend regex, but I thought I would present you with this:

import re
s ='(\\HasNoChildren \\Junk) "/" "[Gmail]/&V4NXPpCuTvY-"'

partpat = re.compile(r'(\(.+\)) "(.+)" "(.+)"')
partsmatch = partpat.search(s)
if partsmatch is None:
     print('No matches.')
else:
     print(partsmatch.groups())


# Results (a Tuple of strings):
('(\\HasNoChildren \\Junk)', '/', '[Gmail]/&V4NXPpCuTvY-')

The pattern says you're looking for: (this thing) "this" "this"

-- 
\¯\      /¯/\
  \ \/¯¯\/ / / Christopher Welborn (cj)
   \__/\__/ /  cjwelborn at live·com
    \__/\__/   http://welbornprod.com

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


Thread

Re: more simple to split the string? Christopher Welborn <cjwelborn@live.com> - 2014-08-07 20:29 -0500

csiph-web