Regular expression help

Discussion in 'Ruby' started by Guest, Apr 17, 2007.

  1. Guest

    Guest Guest

    I have a line such as:

    #define BLAH 10

    or

    #include "BLAH.h"

    and I'm going around in circles trying to just get the 'include' or
    'define' bit...anyone can help me?

    Thanks
    Bob.

    --
    Posted via http://www.ruby-forum.com/.
     
    Guest, Apr 17, 2007
    #1
    1. Advertising

  2. Guest

    Peter Szinek Guest

    Guest wrote:
    > I have a line such as:
    >
    > #define BLAH 10
    >
    > or
    >
    > #include "BLAH.h"
    >
    > and I'm going around in circles trying to just get the 'include' or
    > 'define' bit...anyone can help me?


    line.scan(/(#.+?) /)[0][0]

    HTH,
    Peter
    __
    http://www.rubyrailways.com :: Ruby and Web2.0 blog
    http://scrubyt.org :: Ruby web scraping framework
    http://rubykitchensink.ca/ :: The indexed archive of all things Ruby
     
    Peter Szinek, Apr 17, 2007
    #2
    1. Advertising

  3. On 4/17/07, Guest <> wrote:
    > I have a line such as:
    >
    > #define BLAH 10
    >
    > or
    >
    > #include "BLAH.h"
    >
    > and I'm going around in circles trying to just get the 'include' or
    > 'define' bit...anyone can help me?


    Not quite sure what you mean - could you show some example of using
    this, and the expected output?

    martin
     
    Martin DeMello, Apr 17, 2007
    #3
  4. On 17.04.2007 16:08, Guest wrote:
    > I have a line such as:
    >
    > #define BLAH 10
    >
    > or
    >
    > #include "BLAH.h"
    >
    > and I'm going around in circles trying to just get the 'include' or
    > 'define' bit...anyone can help me?


    >> ["#define BLAH 10", '#include "BLAH.h"'].map {|s| s[/^#(\w+)/, 1]}

    => ["define", "include"]

    HTH

    robert
     
    Robert Klemme, Apr 17, 2007
    #4
  5. Guest

    Guest

    On Tue, 17 Apr 2007, Guest wrote:

    > I have a line such as:
    >
    > #define BLAH 10
    >
    > or
    >
    > #include "BLAH.h"
    >
    > and I'm going around in circles trying to just get the 'include' or 'define'
    > bit...anyone can help me?
    >
    > Thanks
    > Bob.


    harp:~ > cat a.rb
    lines =
    '#define BLAH 10',
    '#include "BLAH.h"',
    ' #define BLAH 10',
    ' # include "BLAH.h"'

    re = %r/^\s*(#)\s*(\w+)\s*(.*)$/o

    lines.each do |line|
    match, pound, directive, rest = re.match(line).to_a
    p [pound, directive, rest] if match
    end


    harp:~ > ruby a.rb
    ["#", "define", "BLAH 10"]
    ["#", "include", "\"BLAH.h\""]
    ["#", "define", "BLAH 10"]
    ["#", "include", "\"BLAH.h\""]

    -a
    --
    be kind whenever possible... it is always possible.
    - the dalai lama
     
    , Apr 17, 2007
    #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. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,247
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    442
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,397
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    886
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,053
    vforvikash
    Dec 29, 2008
Loading...

Share This Page