Python Forum
splitting a string with 2 different delimiters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting a string with 2 different delimiters
#1
i have a big string with 2 different delimiter characters. i want to split this big string into a list of strings for both delimiters. also, i want to keep the delimiter character at the beginning of each string it start a new string at.

with delimiters "XY", "XfooYbar" -> ["Xfoo","Ybar"]

i could make a function that does this character-by-character. but that might be too slow for the 100's of millions of characters i expect this to work with. this project needs to be limited to what comes with Python and what i can include in the code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Hi,
One easy solution:
string = "XfooYbar"
pars1 = "X"
pars2 = "Y"
final = []
xpos = string.find("X")
ypos = string.find("Y")
final.append(string[xpos:ypos])
final.append(string[ypos:])
print(final)

Or a better solution with Regex:
import re
string = "XfooYbar"
regex = re.compile('^(X[^Y]+)(Y[^X]+)$')
final2 = []
m = regex.match(string)
[final2.append(items) for items in m.group(1, 2)]
print(final2)
Reply
#3
Try this regex "[-.]+". The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.
Reply
#4
@BamBi25

i modified the test string:
import re
string = "XfooYbarXbooYfar"
regex = re.compile('^(X[^Y]+)(Y[^X]+)$')
final2 = []
m = regex.match(string)
[final2.append(items) for items in m.group(1, 2)]
print(final2)
which seems to have a problem:
Output:
t2a/forums /home/forums 15> cat -n foo.py 1 import re 2 string = "XfooYbarXbooYfar" 3 regex = re.compile('^(X[^Y]+)(Y[^X]+)$') 4 final2 = [] 5 m = regex.match(string) 6 [final2.append(items) for items in m.group(1, 2)] 7 print(final2) lt2a/forums /home/forums 16> py3 foo.py Traceback (most recent call last): File "foo.py", line 6, in <module> [final2.append(items) for items in m.group(1, 2)] AttributeError: 'NoneType' object has no attribute 'group' lt2a/forums /home/forums 17>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Try this
re.findall(r'(X[^Y]+)(Y[^X]+)', string)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string between two different delimiters, with exceptions DreamingInsanity 2 353 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  Split string with multiple delimiters and keep the string in "groups" DreamingInsanity 4 689 May-12-2020, 09:31 AM
Last Post: DeaD_EyE
  Splitting a string twice bazcurtis 2 762 Mar-09-2020, 02:54 PM
Last Post: perfringo
  Problem with delimiters johnprada 5 670 Jan-29-2020, 10:17 AM
Last Post: DeaD_EyE
  Problem with delimiters johnprada 1 400 Jan-28-2020, 04:27 PM
Last Post: buran
  Split a long string into other strings with no delimiters/characters krewlaz 4 636 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Splitting String into 2d list cclark135 2 784 Aug-26-2019, 01:46 PM
Last Post: ThomasL
  re.split multiple delimiters problem gw1500se 2 1,461 Jun-24-2019, 02:43 PM
Last Post: gw1500se
  Strange behaviour while splitting string? naknak12 2 845 Feb-18-2019, 01:57 PM
Last Post: naknak12
  Finding nested delimiters wfsteadman 4 1,489 Jan-22-2018, 07:23 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020