Parsing text

Discussion in 'Ruby' started by Cyril Jose, Apr 22, 2011.

  1. Cyril Jose

    Cyril Jose Guest

    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/.
    Cyril Jose, Apr 22, 2011
    #1
    1. Advertising

  2. [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 <> 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
    John W Higgins, Apr 22, 2011
    #2
    1. Advertising

  3. Cyril Jose

    7stud -- Guest

    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/.
    7stud --, Apr 22, 2011
    #3
  4. Cyril Jose

    Cyril Jose Guest

    Cyril Jose, Apr 22, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    869
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    571
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    600
    Christopher Diggins
    Jul 9, 2007
  4. Kai Schlamp
    Replies:
    1
    Views:
    410
    Arne Vajhøj
    Mar 27, 2008
  5. Domenico Discepola

    Assistance parsing text file using Text::CSV_XS

    Domenico Discepola, Sep 1, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    444
    Domenico Discepola
    Sep 2, 2004
Loading...

Share This Page