-2

hi how can I split this string while keeping '(':

s = ' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);'
x = s.split("(")
print(x)

I also tried with:

x = s.split("?=(")

but it did not work.

thank you

V.Cozzatella
  • 709
  • 2
  • 9
  • 15
  • 3
    What does 'keeping the separator' means in your case? The separator as a separate element of the resulting list or joined to one of the adjacent elements? – zwer Apr 24 '18 at 09:49
  • Well why do you want to keep separators? If you extract information from string usingg regular expression and group to extract them. – Sumit Jha Apr 24 '18 at 10:17

5 Answers5

0

If you just need to keep the separator, you can re-introduce it after the split:

def split_keep_separator(source, sep):
    elements = source.split(sep)
    return [e for t in zip(elements, [sep] * (len(elements))) for e in t][:-1]

And to test it:

s = ' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);'

x = split_keep_separator(s, "(")
print(x)

# [' playsound3Dwhenpossible',
#  '(',
#  'soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);']
zwer
  • 24,943
  • 3
  • 48
  • 66
0

If you want to keep the separator '(' in the output array, the simplest is to use a regex split.

import re
s = ' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);'
x = re.split(r'(\()', s)
afdezl
  • 26
  • 2
0

Based on your input string, It seems your expected output may be,

[' playsound3Dwhenpossible', '(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);']

If so, this seems easiest to me

s = ' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);'
print s.replace("(","*(").split('*')

First replace and than split.

Instead of '*', you can use whatever, which is NOT in the original string.

Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
0

Another regex-based solution:

import re

s = ' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);'
splits = re.findall(r'(\(?[^(]+)', s)
print(splits)

Output:

[' playsound3Dwhenpossible', '(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);']

Or if you want to have a generic (or easy to use) function:

split_keep = lambda s, sep : re.findall('({0}?[^{0}]+)'.format(re.escape(sep)), s)
splits = split_keep(s, '(')

This fails in the corner case of having multiple contiguous separators. The following slightly more complex function fixes it:

split_keep = lambda s, sep : re.findall('({0}[^{0}]*|{0}?[^{0}]+)'.format(re.escape(sep)), s)
print(split_keep('((asda((asfasf', '('))

Output:

['(', '(asda', '(', '(asfasf']
jdehesa
  • 58,456
  • 7
  • 77
  • 121
0

You could possibly change your split slightly to :

s = ' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);'

x = s.split(r'\(') #raw
print(x)

Output:

[' playsound3Dwhenpossible(soundspotpoint18, %$videos_sounds_path%/sounds/lavazza_-_auguri_cherubini__15_.mp3, true, false, -52.644483, 0.947368, 90, 1, spotpoint18);']
lola
  • 115
  • 2
  • 6