Possible bug in regexp engine?

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

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

On 12 mar, 15:47, Robert K. [email protected] 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

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