regex issue confusing

A

Ahmet Kilic

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

why??? please help.
 
B

Brian Candler

Ahmet said:
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"
 
A

Ahmet Kilic

Brian said:
Ahmet said:
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.
 
A

Ahmet Kilic

Glenn said:
At said:
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.
 
B

Brian Candler

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 said:
line[/^\s*#include\s*["<]([^">]+)/, 1]
=> "stdio.h"

HTH,

Brian.
 
R

Rob Biedenharn

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 said:
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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

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

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top