# Regex issue confusing

**URL:** <https://www.ruby-forum.com/t/regex-issue-confusing/174798>\
**Category:** Ruby\
**Created:** [September 2, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798 "2009-09-02T13:02:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/1 "2009-09-02T13:02:02Z")

</div>

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

why??? please help.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/2 "2009-09-02T14:02:07Z")

</div>

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”
> 
> >

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/3 "2009-09-02T15:26:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/4 "2009-09-02T15:30:42Z")

</div>

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"

```

?

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/5 "2009-09-02T15:39:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 4:50pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/6 "2009-09-02T16:50:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 2, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/regex-issue-confusing/174798/7 "2009-09-02T18:31:38Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
