Parsing text

C

Cyril Jose

Hey all,

I have a file where I need to parse information from. The format of the
first line is something like this:

">ruby ruby |ruby|ruby ruby|text_i_want| test test"

I was thinking converting this line into an array, using the .split(//)
and keeping count of the pipe("|") character so that when it reaches the
3rd one, it reads the characters up till the 4th pipe(all in a do
iterator. So in essence, I would want to extract "text_i_want". When i
tried this method, I got stuck. Any ideas on how to move forward? Or an
easier solution than this? Thanks!
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Good Afternoon,

Hey all,

I have a file where I need to parse information from. The format of the
first line is something like this:

">ruby ruby |ruby|ruby ruby|text_i_want| test test"

I was thinking converting this line into an array, using the .split(//)

You got close - this should work for you

split(/\|/)[3]

That will return the 4th group of text for you

John
 
7

7stud --

A pipe is one of the special regex characters--it does not stand for a
literal pipe. A pipe is used in a regex to mean 'OR'.

There several other ways to escape the special regex characters, so that
they will lose their special meaning and match themselves:

1) You can use a backslash to escape the pipe.

2) You can put the pipe in a character class:

str = ">ruby ruby |ruby|ruby ruby|text_i_want| test test"

pieces = str.split(/[|]/)
puts pieces[3]

--output:--
text_i_want

3) You can call Regexp.escape to escape any special regex characters
contained in the string, so that they lose their special meaning:

str = ">ruby ruby |ruby|ruby ruby|text_i_want| test test"

pattern = "|"
esc_str = Regexp.escape(pattern)

pieces = str.split(/#{esc_str}/)
puts pieces[3]

--output:--
text_i_want
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top