ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api
       [not found] <redmine.issue-15496.20190102085159@ruby-lang.org>
@ 2019-01-02  8:52 ` macdevign
  2019-01-02 10:12 ` [ruby-core:90852] " shevegen
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: macdevign @ 2019-01-02  8:52 UTC (permalink / raw)
  To: ruby-core

Issue #15496 has been reported by macdevign (Macdevign mac).

----------------------------------------
Feature #15496: Extract between string as standard String api
https://bugs.ruby-lang.org/issues/15496

* Author: macdevign (Macdevign mac)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I could not find the a simple String api to extract the string between two string, and I notice that many face the same issue and endup rolling their own solutions (eg https://stackoverflow.com/questions/9661478/how-to-return-the-substring-of-a-string-between-two-strings-in-ruby). 

Given that string "between" extraction is such a common operation, will adding a focused and simplified String method  make coding pleasant ?

This is my solution but probably someone can provide better and efficient implementation.

`
  # self: String instance, 
  # from: first 'from' String  
  # to:  first 'to' String found after 'from'
  # return string between from and to, which exclude the argument, nil otherwise.

  def between(from, to)
      from_idx = self.index(from)&.+(from.length)
      if from_idx
        to_idx = self.index(to, from_idx)
        return self[from_idx...to_idx] if to_idx
      end
      nil
    end
`

Test case

"Hello world".between("ell", "ld") => "o wor"
"Testing 123".between("Te", "123") => "sting "





-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:90852] [Ruby trunk Feature#15496] Extract between string as standard String api
       [not found] <redmine.issue-15496.20190102085159@ruby-lang.org>
  2019-01-02  8:52 ` [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api macdevign
@ 2019-01-02 10:12 ` shevegen
  2019-01-04  6:46 ` [ruby-core:90884] " duerst
  2019-01-05  7:23 ` [ruby-core:90894] " macdevign
  3 siblings, 0 replies; 4+ messages in thread
From: shevegen @ 2019-01-02 10:12 UTC (permalink / raw)
  To: ruby-core

Issue #15496 has been updated by shevegen (Robert A. Heiler).


Hmmm. I don't have a preference here since I can find arguments in favour or against
this. It's fairly easily possible to use a regex so .between (or any other name to 
extract the substring between two boundaries) is not that necessary; on the other hand
it may be simpler to use an "officially approved" method.

If you feel strongly about your proposal you could propose it for mention during a 
developer meeting at:

https://bugs.ruby-lang.org/issues/15462

(In ~a week or so from now on.)

Then you may get matz' opinion about the proposal, both feature-wise and API/name wise.

----------------------------------------
Feature #15496: Extract between string as standard String api
https://bugs.ruby-lang.org/issues/15496#change-76039

* Author: macdevign (Macdevign mac)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I could not find the a simple String api to extract the string between two string, and I notice that many face the same issue and endup rolling their own solutions (eg https://stackoverflow.com/questions/9661478/how-to-return-the-substring-of-a-string-between-two-strings-in-ruby). 

Given that string "between" extraction is such a common operation, will adding a focused and simplified String method  make coding pleasant ?

This is my solution but probably someone can provide better and efficient implementation.

~~~
  # self: String instance, 
  # from: first 'from' String  
  # to:  first 'to' String found after 'from'
  # return string between from and to, which exclude the argument, nil otherwise.

  def between(from, to)
      from_idx = self.index(from)&.+(from.length)
      if from_idx
        to_idx = self.index(to, from_idx)
        return self[from_idx...to_idx] if to_idx
      end
      nil
    end
~~~

Test case

"Hello world".between("ell", "ld") => "o wor"
"Testing 123".between("Te", "123") => "sting "





-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:90884] [Ruby trunk Feature#15496] Extract between string as standard String api
       [not found] <redmine.issue-15496.20190102085159@ruby-lang.org>
  2019-01-02  8:52 ` [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api macdevign
  2019-01-02 10:12 ` [ruby-core:90852] " shevegen
@ 2019-01-04  6:46 ` duerst
  2019-01-05  7:23 ` [ruby-core:90894] " macdevign
  3 siblings, 0 replies; 4+ messages in thread
From: duerst @ 2019-01-04  6:46 UTC (permalink / raw)
  To: ruby-core

Issue #15496 has been updated by duerst (Martin Dürst).


macdevign (Macdevign mac) wrote:

> Given that string "between" extraction is such a common operation,

Can you back that up with some additional information/data? For example, do you know other programming languages that have such a function/method?

I haven't had the need for such a method, and use cases I can think of very quickly need additional or different parameters (such as xth occurrence, between (Regexp) patterns rather than simple strings, before or after a string,...), which suggests to me that solving this specific problem with the general regular expression functionality already in Ruby.


----------------------------------------
Feature #15496: Extract between string as standard String api
https://bugs.ruby-lang.org/issues/15496#change-76071

* Author: macdevign (Macdevign mac)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I could not find the a simple String api to extract the string between two string, and I notice that many face the same issue and endup rolling their own solutions (eg https://stackoverflow.com/questions/9661478/how-to-return-the-substring-of-a-string-between-two-strings-in-ruby). 

Given that string "between" extraction is such a common operation, will adding a focused and simplified String method  make coding pleasant ?

This is my solution but probably someone can provide better and efficient implementation.

~~~
  # self: String instance, 
  # from: first 'from' String  
  # to:  first 'to' String found after 'from'
  # return string between from and to, which exclude the argument, nil otherwise.

  def between(from, to)
      from_idx = self.index(from)&.+(from.length)
      if from_idx
        to_idx = self.index(to, from_idx)
        return self[from_idx...to_idx] if to_idx
      end
      nil
    end
~~~

Test case

"Hello world".between("ell", "ld") => "o wor"
"Testing 123".between("Te", "123") => "sting "





-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:90894] [Ruby trunk Feature#15496] Extract between string as standard String api
       [not found] <redmine.issue-15496.20190102085159@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-01-04  6:46 ` [ruby-core:90884] " duerst
@ 2019-01-05  7:23 ` macdevign
  3 siblings, 0 replies; 4+ messages in thread
From: macdevign @ 2019-01-05  7:23 UTC (permalink / raw)
  To: ruby-core

Issue #15496 has been updated by macdevign (Macdevign mac).


duerst (Martin Dürst) wrote:
> macdevign (Macdevign mac) wrote:
> 
> > Given that string "between" extraction is such a common operation,
> 
> Can you back that up with some additional information/data? For example, do you know other programming languages that have such a function/method?
> 
> I haven't had the need for such a method, and use cases I can think of very quickly need additional or different parameters (such as xth occurrence, between (Regexp) patterns rather than simple strings, before or after a string,...), which suggests to me that solving this specific problem with the general regular expression functionality already in Ruby.

Yes, I agree that there is regex that can do the work in Ruby.

I use it quite often in data scraping and oftn wonder why need to resort to regex and worry about escaping regex character ?
There is many ways to do the extraction (eg regex) but if there is standard way and convenient way, I wonder why reinvent our own ?  I like the way Ruby provide us the creativity to solve the problem, but such simple "common" task should be simpler :}

Compare to the other languages, I believe Ruby and Python are one of those scripting languages commonly used for data scraping, and it will help if there is simple method facilitate such extraction.

Just googling the for "extract between " for language such as python, javascript, java, etc, one can see this is common question,

If other languages didn't provide it, probably Ruby can set the catalyst for other language to include it ?


Readability wise, extraction through method seems easier on the eyes, but that could be just me :}

  "The quick brown fox jumps over the lazy dog"[/jumps(.+)dog/,1]   # regex
  "The quick brown fox jumps over the lazy dog".between("jumps", "dog")   # method

thank





----------------------------------------
Feature #15496: Extract between string as standard String api
https://bugs.ruby-lang.org/issues/15496#change-76081

* Author: macdevign (Macdevign mac)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I could not find the a simple String api to extract the string between two string, and I notice that many face the same issue and endup rolling their own solutions (eg https://stackoverflow.com/questions/9661478/how-to-return-the-substring-of-a-string-between-two-strings-in-ruby). 

Given that string "between" extraction is such a common operation, will adding a focused and simplified String method  make coding pleasant ?

This is my solution but probably someone can provide better and efficient implementation.

~~~
  # self: String instance, 
  # from: first 'from' String  
  # to:  first 'to' String found after 'from'
  # return string between from and to, which exclude the argument, nil otherwise.

  def between(from, to)
      from_idx = self.index(from)&.+(from.length)
      if from_idx
        to_idx = self.index(to, from_idx)
        return self[from_idx...to_idx] if to_idx
      end
      nil
    end
~~~

Test case

"Hello world".between("ell", "ld") => "o wor"
"Testing 123".between("Te", "123") => "sting "





-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-01-05  7:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15496.20190102085159@ruby-lang.org>
2019-01-02  8:52 ` [ruby-core:90851] [Ruby trunk Feature#15496] Extract between string as standard String api macdevign
2019-01-02 10:12 ` [ruby-core:90852] " shevegen
2019-01-04  6:46 ` [ruby-core:90884] " duerst
2019-01-05  7:23 ` [ruby-core:90894] " macdevign

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).