Forum: Ruby '#' characters are breaking my regexp

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 12:24
I'm trying to build a regexp that includes music notes, eg Bb or C#.
However, the '#' character is breaking them - basically it just stops
reading the string as soon as it gets to a #:

>> string = "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
=> "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
>> regex = Regexp.new(string)
=> AbBbDbEbGbA

Can anyone explain why this is happening and how to get around it?  Is
it thinking that the part after the first hash is a comment for example?

thanks, max
F50f5d582d76f98686da34917531fe56?d=identicon&s=25 Peter Szinek (Guest)
on 2008-11-13 12:37
(Received via mailing list)
Max,

> I'm trying to build a regexp that includes music notes, eg Bb or C#.
> However, the '#' character is breaking them - basically it just stops
> reading the string as soon as it gets to a #:
>
>>> string = "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
> => "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
>>> regex = Regexp.new(string)
> => AbBbDbEbGbA

Strange... it works for me:

 >> string = "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
=> "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
 >> /#{string}/
=> /(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)/
 >> Regexp.new(string)
=> /(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)/

what's your Ruby version?

as far as I can remember, # should have no special meaning (unless
followed by a { when it is an interpolation ) so I am not sure what's
going on...



Cheers,
Peter
___
http://www.rubyrailways.com
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 13:14
Hi Peter.  We're still on 1.8.6 round here.
F50f5d582d76f98686da34917531fe56?d=identicon&s=25 Peter Szinek (Guest)
on 2008-11-13 13:30
(Received via mailing list)
On 2008.11.13., at 13:11, Max Williams wrote:

> Hi Peter.  We're still on 1.8.6 round here.

Well, me too... I have no idea then. Let's hope someone came across
this already and will help out.

Cheers,
Peter
___
http://www.rubyrailways.com
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 13:44
Peter Szinek wrote:
> On 2008.11.13., at 13:11, Max Williams wrote:
>
>> Hi Peter.  We're still on 1.8.6 round here.
>
> Well, me too... I have no idea then. Let's hope someone came across
> this already and will help out.
>
> Cheers,
> Peter
> ___
> http://www.rubyrailways.com

Bugger.  How annoying...maybe i have some weird gem interference or
something. (facets perhaps).  Don't know how to debug that...

Anyone else?

Thanks!
max
26a81e5badb9e002ab9ed3542036e584?d=identicon&s=25 Michael Libby (Guest)
on 2008-11-13 13:48
(Received via mailing list)
On Thu, Nov 13, 2008 at 5:21 AM, Max Williams
<toastkid.williams@gmail.com> wrote:
> it thinking that the part after the first hash is a comment for example?
The # must be confusing the parser into thinking some #{} style
interpolation is going to happen.

Substitute \# for # and see if that helps. Either way works the same
for me in 1.8.7.

-Michael Libby
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 13:55
Michael Libby wrote:
> On Thu, Nov 13, 2008 at 5:21 AM, Max Williams
> <toastkid.williams@gmail.com> wrote:
>> it thinking that the part after the first hash is a comment for example?
> The # must be confusing the parser into thinking some #{} style
> interpolation is going to happen.
>
> Substitute \# for # and see if that helps. Either way works the same
> for me in 1.8.7.
>
> -Michael Libby

I tried that already, no joy :(

>> string = "abc#def"
=> "abc#def"
>> /#{string}/
=> abc
>> string = "abc\#def"
=> "abc#def"
>> /#{string}/
=> abc
>> Regexp.new(string)
=> abc
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-11-13 14:30
(Received via mailing list)
>>> string = "abc#def"
> => "abc#def"
>>> /#{string}/
> => abc
>>> string = "abc\#def"

"abc\\#def"

When you mash like /#{string}/, the inserted item is not treated as a
literal string, but as a fragment of Regexp notation. So either first
call
Regexp.escape(string) on it, or make it a valid regexp.

Next, "" might interpret \# as an escape in string notation, so escape
_that_ with \\#.

And I always use '' unless I need the special powers of a "". So
'abc\#def'
will work, because '' refrains from escaping anything that it doesn't
need
to.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 14:36
And it works fine on our server...it's just on mine and my colleagues'
local machines that it doesn't work.  Aiiieeeee....
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-13 14:39
Max Williams wrote:
> Bugger.  How annoying...maybe i have some weird gem interference or
> something. (facets perhaps).  Don't know how to debug that...

1. Run plain irb, don't load any libraries yet,
2. Try the example from the command line, see if it works.
3. If it does, start loading libraries until it breaks.

From the prompt, it looks to me like you're using script/console in
Rails, so you'll have all the Rails stuff loaded too, like
ActiveSupport. But that doesn't cause this problem for me:

$ script/console
Loading development environment (Rails 2.1.2)
>> string = "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
=> "(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)"
>> regex = Regexp.new(string)
=> /(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)/
>>

Nor facets:

>> require 'facets'
=> []
>> regex = Regexp.new(string)
=> /(Ab|Bb|Db|Eb|Gb|A#|C#|D#|F#|G#|A|B|C|D|E|F|G)/
>>
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 14:39
Phlip wrote:
>
> "abc\\#def"
>
> When you mash like /#{string}/, the inserted item is not treated as a
> literal string, but as a fragment of Regexp notation. So either first
> call
> Regexp.escape(string) on it, or make it a valid regexp.
>
> Next, "" might interpret \# as an escape in string notation, so escape
> _that_ with \\#.
>
> And I always use '' unless I need the special powers of a "". So
> 'abc\#def'
> will work, because '' refrains from escaping anything that it doesn't
> need
> to.

Still no joy i'm afraid:

>> string = 'abc#def'
=> "abc#def"
>> /#{string}/
=> abc
>> Regexp.escape(string)
=> "abc\\#def"
>> Regexp.new(Regexp.escape(string))
=> abc
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2008-11-13 14:45
(Received via mailing list)
Le 13 novembre à 13:52, Max Williams a écrit :

>>> string = "abc#def"
> => "abc#def"
>>> /#{string}/
> => abc
>>> string = "abc\#def"
> => "abc#def"
>>> /#{string}/
> => abc
>>> Regexp.new(string)
> => abc

Are you sure it's not simply a display problem in IRB ?

>> s = "abc#def"
=> "abc#def"
>> re = /#{s}/
=> abc
>> re.inspect
=> "/abc#def/"
>> re.match("aaaabc#defffff")
=> #<MatchData:0xca0ee0>
>> re.match("aaaabc#defffff")[0]
=> "abc#def"

After a few tests, it seems it comes from wirble, in my case...

Fred
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2008-11-13 14:50
(Received via mailing list)
On Nov 13, 2008, at 5:34 AM, Peter Szinek wrote:

> as far as I can remember, # should have no special meaning (unless
> followed by a { when it is an interpolation )

# also becomes a comment character inside a Regexp if the /x
(EXTENDED) mode is used.

James Edward Gray II
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2008-11-13 15:01
F. Senault wrote:

> Are you sure it's not simply a display problem in IRB ?
>
>>> s = "abc#def"
> => "abc#def"
>>> re = /#{s}/
> => abc
>>> re.inspect
> => "/abc#def/"
>>> re.match("aaaabc#defffff")
> => #<MatchData:0xca0ee0>
>>> re.match("aaaabc#defffff")[0]
> => "abc#def"
>
> After a few tests, it seems it comes from wirble, in my case...
>
> Fred

That's it exactly!  Fred, you're a genius - it was working properly all
along, it just looked like it wasn't.

Thanks a lot, and to everyone else too.
This topic is locked and can not be replied to.