# Go to specific char in a file

**URL:** <https://www.ruby-forum.com/t/go-to-specific-char-in-a-file/126445>\
**Category:** Ruby\
**Created:** [January 18, 2008, 10:06am UTC](https://www.ruby-forum.com/t/go-to-specific-char-in-a-file/126445 "2008-01-18T10:06:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tabing16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tabing16](https://www.ruby-forum.com/u/tabing16)\
**Post date:** [January 18, 2008, 10:06am UTC](https://www.ruby-forum.com/t/go-to-specific-char-in-a-file/126445/1 "2008-01-18T10:06:09Z")

</div>

Hi

I am looking for a way to get to a specific character in a file.  
Let’s say i have the following lines in a file

ABC12AX3456XYZ  
ABC123HGNasdfg

How do I ask ruby to grab character 3-8 on each line and display them on  
the screen?

I would really appreciate if someone could show it by using regular  
expressions.

thanks

tabing16

---

<div class="post-metadata">

**Author:** ![tabing16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tabing16](https://www.ruby-forum.com/u/tabing16)\
**Post date:** [January 18, 2008, 10:13am UTC](https://www.ruby-forum.com/t/go-to-specific-char-in-a-file/126445/2 "2008-01-18T10:13:16Z")

</div>

Alle Friday 18 January 2008, Rudy Rusli ha scritto:

> I would really appreciate if someone could show it by using regular  
> expressions.
> 
> thanks
> 
> tabing16

Using a regexp is not the clearer way to do it, in my opinion. However,  
you  
can do:

line.match(/^.{3}(.{6})/  
puts $1

The simpler way is:

puts line[3…8]

(I assume that you’re counting characters starting from 0).

Stefano

---

<div class="post-metadata">

**Author:** ![tabing16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tabing16](https://www.ruby-forum.com/u/tabing16)\
**Post date:** [January 18, 2008, 10:32am UTC](https://www.ruby-forum.com/t/go-to-specific-char-in-a-file/126445/3 "2008-01-18T10:32:48Z")

</div>

> Using a regexp is not the clearer way to do it, in my opinion. However,  
> you  
> can do:
> 
> line.match(/^.{3}(.{6})/  
> puts $1
> 
> The simpler way is:
> 
> puts line[3…8]
> 
> (I assume that you’re counting characters starting from 0).
> 
> Stefano

Hi Stefano

thanks for the speedy reply and solving it  
I did try the second method.  
I was just wondering if there’s anyway to tackle it by using reg ex

cheers

tabing16

---

<div class="post-metadata">

**Author:** ![tabing16](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tabing16](https://www.ruby-forum.com/u/tabing16)\
**Post date:** [January 18, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/go-to-specific-char-in-a-file/126445/4 "2008-01-18T16:11:27Z")

</div>

Rudy Rusli wrote:

> I was just wondering if there’s anyway to tackle it by using reg ex

Something like…

> > File.foreach(‘file’) {|line| puts line.match(/^.{3}(.{5})/)[1] }  
> > 12AX3  
> > 123HG

?

Regards,  
Lee
