Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3351 > unrolled thread
| Started by | Cyril Jose <cyril_jose@ymail.com> |
|---|---|
| First post | 2011-04-21 20:43 -0500 |
| Last post | 2011-04-21 22:15 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.ruby
Parsing text Cyril Jose <cyril_jose@ymail.com> - 2011-04-21 20:43 -0500
Re: Parsing text John W Higgins <wishdev@gmail.com> - 2011-04-21 21:08 -0500
Re: Parsing text 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-21 21:20 -0500
Re: Parsing text Cyril Jose <cyril_jose@ymail.com> - 2011-04-21 22:15 -0500
| From | Cyril Jose <cyril_jose@ymail.com> |
|---|---|
| Date | 2011-04-21 20:43 -0500 |
| Subject | Parsing text |
| Message-ID | <75e6c383a0a6a5d072fdbe3ba9a7ceb5@ruby-forum.com> |
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!
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | John W Higgins <wishdev@gmail.com> |
|---|---|
| Date | 2011-04-21 21:08 -0500 |
| Message-ID | <BANLkTinPp52D+E27Xc_5GdySiLWypHHzDg@mail.gmail.com> |
| In reply to | #3351 |
[Note: parts of this message were removed to make it a legal post.] Good Afternoon, On Thu, Apr 21, 2011 at 6:43 PM, Cyril Jose <cyril_jose@ymail.com> wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-21 21:20 -0500 |
| Message-ID | <bda716b2dae1faf21249aeefb9ee2902@ruby-forum.com> |
| In reply to | #3351 |
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
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Cyril Jose <cyril_jose@ymail.com> |
|---|---|
| Date | 2011-04-21 22:15 -0500 |
| Message-ID | <8d2f28326e89189f123c58b02db9bb09@ruby-forum.com> |
| In reply to | #3351 |
Thanks John and 7stud - I have a better understanding now. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web