Python Forum
substring between substrings
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
substring between substrings
#1
i am looking for an existing function or i will code my own. given a main string and two substrings A and B ... if A and B are both in the main string and B is found after the end of A, return the substring found between A and B. if B is immediately after A then return an empty string. a way to also do this with byte strings is a big plus, even if it is a different function name. note that A and B can be strings longer than a single character and that A and B may be the same or may be different.

m = 'happy birthday to you'
a = 'happy '
b = ' to'
betweenstr(m,a,b) -> 'birthday'
a = 'b'
b = 'y'
betweenstr(m,a,b) -> 'irthda'
a = 'birth'
b = 'day'
betweenstr(m,a,b) -> ''
a = 'day'
b = 'happy'
betweenstr(m,a,b) -> None or an exception
i am not asking anyone to code this for me; i can do that. i am only interested in something in the Python library or i code on my own, not somehing to be installed.
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
substring between substrings - by Skaperen - Oct-27-2018, 12:01 AM
RE: substring between substrings - by ichabod801 - Oct-27-2018, 02:08 AM
RE: substring between substrings - by Skaperen - Oct-27-2018, 03:51 AM
RE: substring between substrings - by wavic - Oct-27-2018, 06:02 AM
RE: substring between substrings - by Skaperen - Oct-27-2018, 06:45 PM
RE: substring between substrings - by ichabod801 - Oct-27-2018, 08:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 632 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,162 Apr-08-2023, 06:36 PM
Last Post: Winfried
  ValueError: substring not found nby2001 4 8,123 Aug-08-2022, 11:16 AM
Last Post: rob101
  Substitue multiple substrings in one command Pavel_47 0 891 Jul-18-2022, 01:24 PM
Last Post: Pavel_47
  Match substring using regex Pavel_47 6 1,530 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Substring Counting shelbyahn 4 6,240 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Find string between two substrings, in a stream of data xbit 1 2,217 May-09-2021, 03:32 PM
Last Post: bowlofred
  Python Substring muzikman 4 2,394 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  Using lambdas and map() to parse substrings in a single line Drone4four 5 3,169 Sep-20-2020, 10:38 AM
Last Post: snippsat
  Removing items from list if containing a substring pythonnewbie138 2 2,267 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

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