go to specific char in a file

R

Rudy Rusli

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
 
S

Stefano Crocco

Alle Friday 18 January 2008, Rudy Rusli ha scritto:
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

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
 
R

Rudy Rusli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top