more simple to split the string?

Christopher Welborn cjwelborn at live.com
Thu Aug 7 21:29:37 EDT 2014


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




More information about the Python-list mailing list