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 #:
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 #:
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
···
On Thu, Nov 13, 2008 at 5:21 AM, Max Williams <toastkid.williams@gmail.com> wrote:
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 #:
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.
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
···
Le 13 novembre à 13:52, Max Williams a écrit :
--
Courage is not the absence of fear,
but rather the judgement that
something else is more important than fear.
(Ambrose Redmoon)
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.
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)