Split text based on character, but keep 'splitting character'

Hi,

Question:
in GH we have the split text component, which splits text on specific text characters given.

However, in the output, the actual splitting character is ‘deleted’.
My question is if somebody knows a way or a workaround to do this.

This topic is also discussed below, with a python solution:

However I havent been able to get a working component just yet.

2023-01-15 split text and keep splitting character.gh (7.1 KB)

Any suggestions?

Many thanks


2023-01-15 split text and keep splitting character_re.gh (8.9 KB)

2 Likes

I have this monstrosity to offer, that works for several separators in the same text (it’s sort of a Shatter List !)

2023-01-15 split text and keep splitting character.gh (7.1 KB)

1 Like

2023-01-15 split text and keep splitting character_mrtn.gh (12.2 KB)

2 Likes

Hi!

thanks for your quick reply :slight_smile:
Unforunately is has also to work when the splitting character is present in multiple original items, see e.g. below.

Any suggestions?

Hi,

Also thanks for your quick reply.
unfortunately same as HS_kim, it has to work for multiple items…

any suggestions?

Hi,

Thanks, I downloaded the file, but is seems its the original file?
I will test the new one :slight_smile:

Thanks!

2023-01-15 split text and keep splitting character_mrtn.gh (14.4 KB)

1 Like

Hi Thanks, again!

Almost there!
It is also possible that the items are present multiple times in 1 item. Any suggestions?

thanks!

Is there somebody who know, how to add this python script in GRasshopper?
I think this is exactly what I would need :slight_smile:

Thanks again :slight_smile:

1 Like

Forgot to save… :man_facepalming:

I modified it so that all splitting characters are applied to each ine of text - your original post let presume that there was one character associated with each string.

2023-01-15 split text and keep splitting character.gh (17.9 KB)

2023-01-15 split text and keep splitting character_mrtn.gh (14.3 KB)

1 Like


2023-01-15 split text and keep splitting character_reV2.gh (14.1 KB)

Hello,

Does something like this work for you?

import re
re.split(r"(?=[,.:;])|(?<=[.,;:]/g)",text)
2 Likes

Hi!

Sorry for my late reply, I think this is possibly the solution I am looking for.
Unfortunately I am not an Python expert.

Would it be possible to write it in an GH - Python component, with 2 input variabels:
x = input text which has to be ‘split’
y = all ‘splitting’ characters at which the text has to be split?

so just like the component below, with the difference that the 'splitting characters should still be present?

Many thanks!
2023-01-28 Split text at chacter ánd keep charcarter.gh (5.9 KB)


[2023-01-28 Split text at chacter ánd keep charcarter.gh|attachment]

If you have a problem and you decide to solve it with regex then you have two problems :wink:

How is this ?

import re

splitters = "".join(y)
pattern = r"(?=[{" + splitters + r"}])|(?<=[" + splitters + r"]/g)"
a = []
for text in x:
    a.append(re.split(pattern,text))
1 Like

Haha thx, again :slight_smile:
I copied it, with the following result:

Any idea how to tweak?

thanks again!

Ahh yes it looks like you need something like this for nested lists… Ghpythonlib tree helpers - #4 by diff-arch

I couldn’t get this to work in a GhPython Script component using regular expression with lookahead (?=…) or lookbehind (?<=…) assertions (these work fine in a C# component).

I was able to get the results @GH-KN wanted with a GhPython Script component, but it took some extra code.

230130_re_split_keep_splitters.gh (12.0 KB)

-Kevin

1 Like