From: Jimmy Thrasher Date: 2001-10-25T22:48:44+09:00 Subject: [ruby-talk:23291] Re: RegExp question At 10:22 PM 10/25/2001 +0900, you wrote: >I need to scan text and treat some punctuation (brackets, slashes) as if >they were word characters, but not others (periods, commas). > >I essentially would like to re-define the set that \w uses then I could >simply use: > > /\w+/ The [] construct makes this possible. If you do punctuationRegex = Regexp.escape("{}/[]") regex = /(?:\w|[#{punctuationRegex}])+/ It should do what you want. What this does is: (?: ) - groups matches without generating backreferences ($1, $2, etc) \w - matches a word character [#{punctuationRegex}] matches any of the characters specified in that string above - "{}/[]". Note that they had to be escaped, so they would be mis-recognized as regex special characters. So, in irb: $ irb irb(main):001:0> punctuationRegex = Regexp.escape("{}/[]") "\\{\\}/\\[\\]" irb(main):002:0> regex = /(?:\w|[#{punctuationRegex}])+/ /(?:\w|[\{\}\/\[\]])+/ irb(main):003:0> "hello{} world".scan(regex) ["hello{}", "world"] Jimmy