'#' characters are breaking my regexp

M

Max Williams

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 #:
=> 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
 
P

Peter Szinek

[Note: parts of this message were removed to make it a legal post.]

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 #:

=> AbBbDbEbGbA

Strange... it works for me:
=> /(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
 
M

Max Williams

Peter said:
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
 
M

Michael Libby

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 #:

=> 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?

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
 
M

Max Williams

Michael said:
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 :(
=> abc
 
P

Phlip

string = "abc#def"
=> "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.
 
M

Max Williams

And it works fine on our server...it's just on mine and my colleagues'
local machines that it doesn't work. Aiiieeeee....
 
B

Brian Candler

Max said:
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)
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)/
 
M

Max Williams

Phlip said:
"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:
=> abc
 
J

James Gray

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
 
M

Max Williams

F. Senault said:
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")
=> # said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top