Regex issue confusing

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

why??? please help.

Ahmet K. 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”

Brian C. wrote:

Ahmet K. 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.

At 2009-09-02 08:02AM, “Brian C.” 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"

?

Glenn J. wrote:

At 2009-09-02 08:02AM, “Brian C.” 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.

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.

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

=> “#include <stdio.h>”

line[/^\s*#include\s*"</, 1]
=> “stdio.h”

HTH,

Brian.

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 B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs