Python Forum
Split string with multiple delimiters and keep the string in "groups"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split string with multiple delimiters and keep the string in "groups"
#3
Is there really something separating each character, or are you just looking for any repeated character? (You use the term "delimiter", but I don't see any).

If you really just want repeated characters, you could do the following. The grouping is a bit odd, but you can pull the repeated strings out of the first part of each tuple.

>>> s = ".nnddd9999999999ddnn."
>>> re.findall(r"((.)\2+)", s)
[('nn', 'n'), ('ddd', 'd'), ('9999999999', '9'), ('dd', 'd'), ('nn', 'n')]
Or if you really just have a few characters and you want all the strings of them, you could do what you did earlier, but add the repetition (+) operator to them:

>>> re.findall(r"(\.+|n+|d+|9+)",s)
['.', 'nn', 'ddd', '9999999999', 'dd', 'nn', '.']
Reply


Messages In This Thread
RE: Split string with multiple delimiters and keep the string in "groups" - by bowlofred - May-11-2020, 02:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I parse the string? anna17 4 378 Apr-10-2024, 10:26 AM
Last Post: DeaD_EyE
  remove gilberishs from a "string" kucingkembar 2 301 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Matching string from a file tester_V 5 498 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  Python3 string slicing Luchano55 4 662 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Retrieve word from string knob 4 534 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 1,041 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  Virtual Env changing mysql connection string in python Fredesetes 0 406 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Comparing Dataframe to String? RockBlok 2 440 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Sample random, unique string pairs from a list without repetitions walterwhite 1 489 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  extract substring from a string before a word !! evilcode1 3 583 Nov-08-2023, 12:18 AM
Last Post: evilcode1

Forum Jump:

User Panel Messages

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