Regular expression help

G

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

Martin DeMello

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
 
R

Robert Klemme

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
 
A

ara.t.howard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top