Python Forum
substring between substrings
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
substring between substrings
#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


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,124 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,171 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