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
#2
I would use regular expressions, like re.compile('(happy )(.*)( to)'). You could easily make a function that generates one for any two strings, and then searches a third string for matches.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
how complicated does this get if A or B has characters that need to be escaped for re?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
>>> m = 'happy birthday to you'
>>> a = 'happy'
>>> b = ' to'
>>> start = m[m.find(a):].lstrip(a)
>>> start
' birthday to you'
>>> start[:start.find(b)]
' birthday'
>>> a = 'birth'
>>> b = 'day'
>>> start = m[m.find(a):].lstrip(a)
>>> start
'day to you'
>>> start[:start.find(b)]
''
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
i can do this without re. i already have. i'm just wondering what all it would take to do it with re. my worry is that strings could have regular expression characters in them so they would need processing to escape those meta characters. i wonder how much code that would involve. here is what i coded:

def between(m,a,b):
    if 'find' not in dir(m):
        return False
    if not isinstance(a,type(m)):
        return False
    if not isinstance(b,type(m)):
        return False
    p = m.find(a)
    if p<0:
        return None
    p += len(a)
    q = m.find(b,p)
    if q<0:
        return None
    return m[p:q]
i would like to make a version of this that works to find the last instance. that is, if A an B appear in the main string more than once, i can get the last one. or maybe a generic verion that gets the Nth instance with negative indexes to count from the end.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
The re module comes with a function specifically for that: re.escape.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


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