# Splitting a string with escapable separator?

**URL:** <https://rubytalk.org/t/splitting-a-string-with-escapable-separator/21159>\
**Category:** ruby-talk\
**Created:** [28 September 2005 00:25 UTC](https://rubytalk.org/t/splitting-a-string-with-escapable-separator/21159 "2005-09-28T00:25:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Sheppard](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Daniel\_Sheppard](https://rubytalk.org/u/Daniel_Sheppard)\
**Post date:** [28 September 2005 00:25 UTC](https://rubytalk.org/t/splitting-a-string-with-escapable-separator/21159/1 "2005-09-28T00:25:25Z")

</div>

Just because you've done something similar before, doesn't mean you  
should trust yourself to code it from memory and get it right without  
testing....

class String  
&nbsp;&nbsp;&nbsp;&nbsp;def split\_escapable(split\_char, escape\_char)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr = []  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;split(split\_char).each do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(arr[-1] && (arr[-1][-1].chr == escape\_char))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[-1].chop!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[-1] \<\< x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr \<\< x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

It's not very good, because you can't escape the backslash, but some  
magic should sort that out, and depending on what you're doing, you  
might not care. Also, it's not all that elegant, so maybe you already  
got this kind of solution.

Also, you should note that here:

&nbsp;&nbsp;"Hello\, World,Hi".split\_escapable(',' '\')  
&nbsp;&nbsp;# =\> ["Hello, World", "Hi"]

Since you're using double-quotes, the backslash is already being  
consumed as an escape character, and it won't compile because the  
backslash in the single quotes needs to be escaped because it preceeds a  
single quote. And also because you missed the comma between arguments.

&nbsp;&nbsp;'Hello\, World,Hi'.split\_escapable(',','\\')  
&nbsp;&nbsp;# =\> ["Hello, World", "Hi"]

works.

> **···**
>
> #####################################################################################  
> This email has been scanned by MailMarshal, an email content filter.  
> #####################################################################################

---

<div class="post-metadata">

**Author:** ![Ben2](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@Ben2](https://rubytalk.org/u/Ben2)\
**Post date:** [28 September 2005 12:25 UTC](https://rubytalk.org/t/splitting-a-string-with-escapable-separator/21159/2 "2005-09-28T12:25:03Z")

</div>

I printed that and put it on my wall. I do that all the time.

-Ben

> **···**
>
> On 9/27/05, Daniel Sheppard \<daniels@pronto.com.au\> wrote:
> 
> > Just because you've done something similar before, doesn't mean you  
> > should trust yourself to code it from memory and get it right without  
> > testing....
