Printing part what's found with a regex

Discussion in 'Ruby' started by John Smith, Sep 17, 2010.

  1. John Smith

    John Smith Guest

    Hi everyone,

    I'm trying to build a program that filters
    certain lines through regular expressions.
    In the seconde regular expression I wan to
    be able to print only what's within the
    \(\w+\) part of the expression?

    How do I do this?

    counter = 0
    if inFile.nil? == false
    File.foreach(inFile) do |line|
    if line =~ /^[^%\s*^%]/
    if line =~ /(?!%)\w+\s+\(\w+\)/
    puts "found in line #{counter+1} "
    puts line
    end
    end
    counter = counter + 1
    end
    end

    Regards,
    Ted.
    --
    Posted via http://www.ruby-forum.com/.
     
    John Smith, Sep 17, 2010
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    You will find that portions of the regex marked with parentheses will be
    places in variables called $1, $2, etc. So, you will want to change the
    regex to

    /(?!%)\w+\s+(\(\w+\))/

    And then the contents of (\(\w+\)) will be placed in the variable $2.

    -Jonathan Nielsen
     
    Jonathan Nielsen, Sep 17, 2010
    #2
    1. Advertising

  3. John Smith

    John Smith Guest

    Jonathan Nielsen wrote:
    > You will find that portions of the regex marked with parentheses will be
    > places in variables called $1, $2, etc. So, you will want to change the
    > regex to
    >
    > /(?!%)\w+\s+(\(\w+\))/
    >
    > And then the contents of (\(\w+\)) will be placed in the variable $2.
    >
    > -Jonathan Nielsen


    hmm I'm trying to print this variable
    but I get nothing. I've tried:

    puts "#$2"
    puts "#{$2}"
    --
    Posted via http://www.ruby-forum.com/.
     
    John Smith, Sep 17, 2010
    #3
  4. John Smith

    John Smith Guest

    John Smith wrote:
    > Jonathan Nielsen wrote:
    >> You will find that portions of the regex marked with parentheses will be
    >> places in variables called $1, $2, etc. So, you will want to change the
    >> regex to
    >>
    >> /(?!%)\w+\s+(\(\w+\))/
    >>
    >> And then the contents of (\(\w+\)) will be placed in the variable $2.
    >>
    >> -Jonathan Nielsen

    >
    > hmm I'm trying to print this variable
    > but I get nothing. I've tried:
    >
    > puts "#$2"
    > puts "#{$2}"


    Ah just realized it is in variable $1.
    (?!%) is a look ahead so I guess it doesn't count as a group?
    --
    Posted via http://www.ruby-forum.com/.
     
    John Smith, Sep 17, 2010
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    On Fri, Sep 17, 2010 at 9:04 AM, John Smith <> wrote:

    > Ah just realized it is in variable $1.
    > (?!%) is a look ahead so I guess it doesn't count as a group?
    >


    Ah yes, that'll be it. Sorry, I wasn't paying attention to what was inside
    that first group. Glad you got it figured out.

    -Jonathan Nielsen
     
    Jonathan Nielsen, Sep 17, 2010
    #5
    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. SMG
    Replies:
    1
    Views:
    2,418
    Kevin
    Apr 26, 2005
  2. Replies:
    2
    Views:
    763
    Jonathan N. Little
    May 13, 2007
  3. Replies:
    3
    Views:
    770
    Reedick, Andrew
    Jul 1, 2008
  4. Jack
    Replies:
    8
    Views:
    283
  5. Printing part of a page

    , Apr 13, 2005, in forum: Javascript
    Replies:
    4
    Views:
    107
Loading...

Share This Page