Forum: Ruby Regular expression help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Guest (Guest)
on 2007-04-17 18:08
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.
Peter S. (Guest)
on 2007-04-17 18:15
(Received via mailing list)
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
Martin DeMello (Guest)
on 2007-04-17 18:20
(Received via mailing list)
On 4/17/07, Guest <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
Robert K. (Guest)
on 2007-04-17 18:41
(Received via mailing list)
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
unknown (Guest)
on 2007-04-17 18:51
(Received via mailing list)
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
This topic is locked and can not be replied to.