Regular expression help

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.

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

On 4/17/07, Guest [email protected] 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

On Tue, 17 Apr 2007, Guest wrote:

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

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