Re: Parsing text



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

.



Relevant Pages

  • Re: [PHP] Re: Space in regex
    ... On 17/11/06, Paul Novitski wrote: ... You don't need to escape the vertical pipe. ... the pattern, the -i pattern modifier is irrelevant, so I'd drop it: ...
    (php.general)
  • Re: Gemini launch tower
    ... It almost looks like an escape ... >>slide, but escape slide wires appeared to be present on ... >Is it a water pipe to hose down the Titan II in case of a propellant leak? ... would fulfill both possible hypotheses of an emergency egress or of ...
    (sci.space.history)
  • Re: Gemini launch tower
    ... It almost looks like an escape ... >> Is it a water pipe to hose down the Titan II in case of a propellant ... > would fulfill both possible hypotheses of an emergency egress or of ... > should read Gunter Wendt's memories again - the 'pad fuehrer' surely ...
    (sci.space.history)
  • Re: [PHP] Re: Space in regex
    ... You don't need to escape the vertical pipe. ... I know ] looks funny but the close-bracket doesn't need to be escaped if it's in the first position, which includes the first position after the negating circumflex. ...
    (php.general)
  • Re: more of a shell / awk question
    ... pipe it into tail -4 so that only the last 4 lines are shown. ... je cherche un poste d'admin UNIX/Mac ...
    (comp.unix.bsd.freebsd.misc)