Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#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 '>'.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Split a long string into other strings with no delimiters/characters krewlaz 4 112 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 241 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  Finding multiple strings between the two same strings Slither 1 281 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to catch string between two strings SriMekala 3 374 May-24-2019, 03:05 AM
Last Post: heiner55
  lists, strings, and byte strings Skaperen 2 1,056 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 1,047 Oct-05-2017, 06:11 PM
Last Post: nilamo
  list of strings to a single string tebirkes 12 4,159 Nov-04-2016, 01:10 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)