Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string between two strings
#1
i want to get the string that is between (the first instances of) two strings, a and b (in that order).  i can do it like:

what_was_between = original_string.split(a)[1].split(b)[0]
is there a shorter, sweeter, and/or more elegant way?  i am dreaming of:

def between(s,a,b):
    return s.split(a)[1].split(b)[0]
should i go with that?
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 think a regular expression is the way to go, if you're looking for simplicity. A regex will also have the advantage of performance, having been optimized in C, and efficiency, doing the minimum string traversal and not needing to create intermediate strings.
Reply
#3
(Dec-15-2016, 05:10 AM)micseydel Wrote: I think a regular expression is the way to go, if you're looking for simplicity. A regex will also have the advantage of performance, having been optimized in C, and efficiency, doing the minimum string traversal and not needing to create intermediate strings.

can that be done in cases where the bounding strings or the string to extract are regular expressions, or subsets or supersets of them?

edit1:

the first (but not the only) project using this will be extracting from between constants '<' and '>'.
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
(Dec-15-2016, 08:39 AM)Skaperen Wrote: the first (ut not the only) project using this will be extracting from between constants '<' and '>'.


http://stackoverflow.com/questions/17323...gs#1732454
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Search multiple CSV files for a string or strings cubangt 7 7,840 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  question: finding multiple strings within string djf123 4 2,899 May-16-2020, 01:00 PM
Last Post: snippsat
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,703 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,716 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  Finding multiple strings between the two same strings Slither 1 2,478 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to catch string between two strings SriMekala 3 2,707 May-24-2019, 03:05 AM
Last Post: heiner55
  lists, strings, and byte strings Skaperen 2 4,181 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  How do I print "string involved" if one or more of my variables are strings? Shellburn 3 3,497 Oct-05-2017, 06:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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