# Possible bug in regexp engine?

**URL:** <https://www.ruby-forum.com/t/possible-bug-in-regexp-engine/88539>\
**Category:** Ruby\
**Created:** [March 12, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/possible-bug-in-regexp-engine/88539 "2007-03-12T14:25:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 12, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/possible-bug-in-regexp-engine/88539/1 "2007-03-12T14:25:35Z")

</div>

Given the following regular expression:

/([^\*#]+|#(?!#|\*)|\*(?!#))+/

I wanted to make it more readable by inserting some comments, so I  
tried adding the “x” option and it no longer compiled:

/([^\*#]+|#(?!#|\*)|\*(?!#))+/x

If you try it in irb you’ll see a message similar to this:

SyntaxError: compile error  
(irb):31: unmatched (: /([^\*#]+|#(?!#|\*)|\*(?!#))+/

To get this to compile I had to add additional backslashes to escape  
the ‘#’ character in the negative lookahead subexpressions:

/([^\*#]+|#(?!#|\*)|\*(?!#))+/x

The ‘#’ character normally matches itself in a regular expression.  
With the “x” option I expect it to have a special meaning (indicating  
a comment) but in one special position (immediately after the opening  
brace and question mark):

(?# comment )

Is this a bug in the regular expression engine, undocumented or am I  
missing something?

No big deal, the thing is compiling, but I’d like to understand this a  
bit better.

Cheers,  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 12, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/possible-bug-in-regexp-engine/88539/2 "2007-03-12T15:50:50Z")

</div>

On 12.03.2007 14:22, Greg H. wrote:

> a comment) but in one special position (immediately after the opening  
> brace and question mark):
> 
> (?# comment )
> 
> Is this a bug in the regular expression engine, undocumented or am I  
> missing something?
> 
> No big deal, the thing is compiling, but I’d like to understand this a  
> bit better.

A “#” all by itself introduces a line comment as in normal code. So  
everything after it is treated as a comment:

15:43:52 [~]: ruby -e ‘r=/foo#bar  
baz/x; p r; p r =~ “foobaz”’  
/foo#bar  
baz/x  
0

The regexp matches “foobaz”. /x allows line comments - (?#…) works  
always:

15:46:08 [~]: ruby -e ‘r=/foo(?#bar)baz/; p r; p r =~ “foobaz”’  
/foo(?#bar)baz/  
0

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 12, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/possible-bug-in-regexp-engine/88539/3 "2007-03-12T17:06:03Z")

</div>

On 12 mar, 15:47, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A “#” all by itself introduces a line comment as in normal code. So  
> everything after it is treated as a comment

Thanks for the info, Robert.

Cheers,  
Greg
