From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Parsing text Date: Thu, 21 Apr 2011 21:20:19 -0500 Organization: Service de news de lacave.net Lines: 35 Message-ID: References: <75e6c383a0a6a5d072fdbe3ba9a7ceb5@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1303438839 26452 65.111.164.187 (22 Apr 2011 02:20:39 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 22 Apr 2011 02:20:39 +0000 (UTC) In-Reply-To: <75e6c383a0a6a5d072fdbe3ba9a7ceb5@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 382041 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!talisker.lacave.net!lacave.net!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:3354 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/.