Python Forum
splitting on 2 or more possible characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting on 2 or more possible characters
#1
this is easy enough to do, but IMHO, makes for some ugly code, compared to splitting on a single character (call split()). a string might have "foo-bar" or it might have "foo_bar". what i want to do is split it on either "-" or "_" so that i get ["foo","bar"] for both cases. there will never be a case of both "-" and "_" together in one string. but i might have more than 2 characters to split on. the splitter characters can be provided in whatever form works best (a list, or tuple, or set, or frozenset, or string). ideally, i'd like to do this in one line without making a function or class but i suspect making a function will be best.

then as the next split thing i'd like to split on a substring, such as "foo123bar" being by "123" to get ["foo","bar"]. i have put any thought into this, yet, but i'd guess there are many implementations of a wide spectrum of quality.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
splitting on 2 or more possible characters - by Skaperen - Sep-02-2018, 03:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove escape characters / Unicode characters from string DreamingInsanity 5 14,446 May-15-2020, 01:37 PM
Last Post: snippsat
  splitting or parsing control characters Skaperen 19 11,581 May-26-2017, 07:27 PM
Last Post: zivoni
  splitting a string by 2 characters Skaperen 8 9,180 Dec-27-2016, 06:14 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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