Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3354

Re: Parsing text

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Parsing text
Date 2011-04-21 21:20 -0500
Organization Service de news de lacave.net
Message-ID <bda716b2dae1faf21249aeefb9ee2902@ruby-forum.com> (permalink)
References <75e6c383a0a6a5d072fdbe3ba9a7ceb5@ruby-forum.com>

Show all headers | View raw


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/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web