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


Groups > comp.lang.python > #75860 > unrolled thread

Re: more simple to split the string?

Started byChristopher Welborn <cjwelborn@live.com>
First post2014-08-07 20:29 -0500
Last post2014-08-07 20:29 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#75860 — Re: more simple to split the string?

FromChristopher Welborn <cjwelborn@live.com>
Date2014-08-07 20:29 -0500
SubjectRe: more simple to split the string?
Message-ID<mailman.12732.1407461393.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web