From: Ryan Davis Date: 2010-04-16T05:31:42+09:00 Subject: [ruby-core:29540] Re: [Bug #3152] StringScanner is failing with symbols like # or { On Apr 15, 2010, at 07:52 , Petr Kovar wrote: > Bug #3152: StringScanner is failing with symbols like # or { > http://redmine.ruby-lang.org/issues/show/3152 > > Author: Petr Kovar > Status: Open, Priority: Normal > ruby -v: all versions > > StringScanner has problems with symbols like # or { in the middle of a text. > If a string starts with the symbols, it's ok. > Function scan_until, however, works well. Problematic functions are > scan and scan_full. > > Example: > > require 'strscan' > > s1 = StringScanner.new('#{expr} blah blah') > s2 = StringScanner.new('blah blah #{expr} blah blah') > reg = /#\{/ > > puts "s1 failed" unless s1.scan(reg) # should be ok > puts "s2 failed" unless s2.scan(reg) # is failing StringScanner#scan != String#scan You need to get the scanner up to the "#{" in s2 for it to work: require 'strscan' s1 = StringScanner.new('#{expr} blah blah') s2 = StringScanner.new('blah blah #{expr} blah blah') reg = /#\{/ puts "s1 failed" unless s1.scan(reg) # should be ok s2.scan(/(?:blah )+/) # need to puts "s2 failed" unless s2.scan(reg) # is failing See ri for another example: > % ri StringScanner.scan > = StringScanner.scan > > Tries to match with pattern at the current position. If there's a match, the > scanner advances the "scan pointer" and returns the matched string. Otherwise, > the scanner returns nil. > > s = StringScanner.new('test string') > p s.scan(/\w+/) # -> "test" > p s.scan(/\w+/) # -> nil > p s.scan(/\s+/) # -> " " > p s.scan(/\w+/) # -> "string" > p s.scan(/./) # -> nil