Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Possible bug in regexp engine?

Reply
Thread Tools

Possible bug in regexp engine?

 
 
Greg Hurrell
Guest
Posts: n/a
 
      03-12-2007
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-12-2007
On 12.03.2007 14:22, Greg Hurrell wrote:
> 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.


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
 
Reply With Quote
 
 
 
 
Greg Hurrell
Guest
Posts: n/a
 
      03-12-2007
On 12 mar, 15:47, Robert Klemme <shortcut...@googlemail.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57