# Splitting on a backtick

**URL:** <https://www.ruby-forum.com/t/splitting-on-a-backtick/238488>\
**Category:** Ruby\
**Created:** [January 13, 2014, 7:50pm UTC](https://www.ruby-forum.com/t/splitting-on-a-backtick/238488 "2014-01-13T19:50:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [January 13, 2014, 7:50pm UTC](https://www.ruby-forum.com/t/splitting-on-a-backtick/238488/1 "2014-01-13T19:50:28Z")

</div>

Hi everyone. I am trying to process a text file in which someone used a  
backtick ( ` ) to separate fields of each line. I can’t seem to  
“escape” the backtick in my code when trying to use the split()  
functionality (want to split the line of text using the backtick as a  
separator).

Any advice? Thanks!

---

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [January 13, 2014, 8:01pm UTC](https://www.ruby-forum.com/t/splitting-on-a-backtick/238488/2 "2014-01-13T20:01:37Z")

</div>

On Mon, Jan 13, 2014 at 10:50 AM, Dan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone. I am trying to process a text file in which someone used a  
> backtick ( ` ) to separate fields of each line. I can’t seem to  
> “escape” the backtick in my code when trying to use the split()  
> functionality (want to split the line of text using the backtick as a  
> separator).

2.1.0 (main):0 \> “foo`bar`baz”  
=\> “foo`bar`baz”  
2.1.0 (main):0 \> “foo`bar`baz”.split /`/  
=\> [  
[0] “foo”,  
[1] “bar”,  
[2] “baz”  
]  
2.1.0 (main):0 \>

Do you have a test case that demonstrates this not working?

---

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [January 13, 2014, 8:54pm UTC](https://www.ruby-forum.com/t/splitting-on-a-backtick/238488/3 "2014-01-13T20:54:18Z")

</div>

Strange - when I use IRB - I get this:

2.0.0-p247 :001 \> line = “hello`there" => "hello`there”  
2.0.0-p247 :002 \> line.split(’`') => ["hello", "there"] 2.0.0-p247 :003 > line.split /`/  
=\> [“hello”, “there”]

But in my script, if I do this:

lineparts = line.split /`/

or this:

lineparts = line.split(’`’)

when I run it, I get this error:

superscrubber.rb:172:in `split': invalid byte sequence in UTF-8 (ArgumentError) from superscrubber.rb:172:in`block in ’  
from superscrubber.rb:171:in `each' from superscrubber.rb:171:in`’

---

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [January 13, 2014, 9:12pm UTC](https://www.ruby-forum.com/t/splitting-on-a-backtick/238488/4 "2014-01-13T21:12:18Z")

</div>

Dan:

It’s not a backtick problem, it’s your source.

Read this very enlightening blog entry on your problem:

> **[Fight back UTF-8 Invalid Byte Sequences](https://thoughtbot.com/blog/fight-back-utf-8-invalid-byte-sequences)**
>
> Chances are, some of you have run into the issue with the invalid byte...

Wayne
