Regex issue confusing

This is OK
line[/\sclass\s(\w+)/, 1]
but
line[/\s#include\s(\w+)/, 1]
this is not.

why??? please help.

···

--
Posted via http://www.ruby-forum.com/.

Ahmet Kilic wrote:

This is OK
line[/\sclass\s(\w+)/, 1]
but
line[/\s#include\s(\w+)/, 1]
this is not.

why??? please help.

"Not OK" by what definition?

Try posting an irb transcript which demonstrates your problem with
sample data. It looks OK to me:

line = "foo #include bar baz"

=> "foo #include bar baz"

line[/\s#include\s(\w+)/, 1]

=> "bar"

···

--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote:

Ahmet Kilic wrote:

This is OK
line[/\sclass\s(\w+)/, 1]
but
line[/\s#include\s(\w+)/, 1]
this is not.

why??? please help.

"Not OK" by what definition?

Try posting an irb transcript which demonstrates your problem with
sample data. It looks OK to me:

line = "foo #include bar baz"

=> "foo #include bar baz"

line[/\s#include\s(\w+)/, 1]

=> "bar"

sorry it was my fault,
i did not recognize "" this.
I corrected it.
irb(main):040:0> line = "foo #include \"bar\" baz"
irb(main):043:0> line[/\s#include\s(\"\w+)/, 1]
=> "\"bar"

thank you very very much.

···

--
Posted via http://www.ruby-forum.com/\.

require 'mind_reading'

Perhaps the line is something like: #include <stdio.h>
in which case the angle bracket gets in the way.

Ahmet, do you want:

    line[/\s*#include\s+['"<]?(\w+)/, 1] # => "stdio"

?

···

At 2009-09-02 08:02AM, "Brian Candler" wrote:

Ahmet Kilic wrote:
> This is OK
> line[/\sclass\s(\w+)/, 1]
> but
> line[/\s#include\s(\w+)/, 1]
> this is not.
>
> why??? please help.

"Not OK" by what definition?

Try posting an irb transcript which demonstrates your problem with
sample data. It looks OK to me:

>> line = "foo #include bar baz"
=> "foo #include bar baz"
>> line[/\s#include\s(\w+)/, 1]
=> "bar"

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

Glenn Jackman wrote:

···

At 2009-09-02 08:02AM, "Brian Candler" wrote:

Try posting an irb transcript which demonstrates your problem with
sample data. It looks OK to me:

>> line = "foo #include bar baz"
=> "foo #include bar baz"
>> line[/\s#include\s(\w+)/, 1]
=> "bar"

require 'mind_reading'

Perhaps the line is something like: #include <stdio.h>
in which case the angle bracket gets in the way.

Ahmet, do you want:

    line[/\s*#include\s+['"<]?(\w+)/, 1] # => "stdio"

?

I am still learner.
really sorry. I did not really recognize it.
--
Posted via http://www.ruby-forum.com/\.

You may find \b useful: it means "match a word boundary" without
actually consuming any characters. Your example starts with \s (match
one space), so it wouldn't match #include right at the beginning of the
file.

But if I remember my C right, the #include has to be at the start of a
line anyway. So perhaps you want:

line = "#include <stdio.h>"

=> "#include <stdio.h>"

line[/^\s*#include\s*["<]([^">]+)/, 1]

=> "stdio.h"

HTH,

Brian.

···

--
Posted via http://www.ruby-forum.com/\.

And if you are trying to examine/process C code (and headers), the # must be at the start of the line, but the preprocessor keyword can be separated from the # by optional white space. For example:

#ifndef MAXCHAR /* bytes to try to read in at once */
# ifdef i386
# define MAXCHAR 65536 /* might be too big for some systems/devices */
# else
# define MAXCHAR 32768
# endif
#endif

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Sep 2, 2009, at 10:50 AM, Brian Candler wrote:

You may find \b useful: it means "match a word boundary" without
actually consuming any characters. Your example starts with \s (match
one space), so it wouldn't match #include right at the beginning of the
file.

But if I remember my C right, the #include has to be at the start of a
line anyway. So perhaps you want:

line = "#include <stdio.h>"

=> "#include <stdio.h>"

line[/^\s*#include\s*["<]([^">]+)/, 1]

=> "stdio.h"

HTH,

Brian.
--