# Regular expression help

**URL:** <https://www.ruby-forum.com/t/regular-expression-help/92256>\
**Category:** Ruby\
**Created:** [April 17, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/regular-expression-help/92256 "2007-04-17T16:08:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/regular-expression-help/92256/1 "2007-04-17T16:08:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/regular-expression-help/92256/2 "2007-04-17T16:15:32Z")

</div>

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](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/regular-expression-help/92256/3 "2007-04-17T16:20:32Z")

</div>

On 4/17/07, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/regular-expression-help/92256/4 "2007-04-17T16:51:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/regular-expression-help/92256/5 "2007-04-17T16:41:34Z")

</div>

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
