From: "Brian Schröder" Date: 2005-07-31T04:25:36+09:00 Subject: Re: [Brainstorming Input] Ruby-Oniguruma interoperability on Named Groups On 30/07/05, Wolfgang N�dasi-Donner wrote: > Let me first explain the reason for and the kind of this message. > > I have an vague idea on coming to more readable Regular Expression, and the possibility to build Libraries of > Regular Expressions. The hook are the named groups ('(?...)') which are part of Oniguruma. The idea was > influenced by the ancient Snobol4-Language and its '*'-operator for unevaluated expressions. > > This input is brainstorming material and not a change proposal, because it is not mature enough. I hope that > something like this will appear sometimes in the future in Ruby. > > Now the idea. > > Ruby and Onigurama should be extended somehow to allow Ruby-objects (usually regular expressions) to be > registered somehow to the class Regexp, so that they can be referenced later in regular expressions. > > In detail a regular expression that consists only of a named group definition (starts with '(?') kann be > registered by something like 'Regex.register(/(?a|b|c|d)/)', and be deleted by > Regex.remove(''). If the regular expression is assigned to a variable this can be used, how to manage > this in the 'remove' case has to be clearified. I used class methods for this example, but it may be better to > introduce a named Regexp objects which will be created by something like '/(?a|b|c|d)/.create. Some > possibility for explicit deletion should be there, because the regex engine Oniguruma must know about the > object to take care about. > > These Object can later on be referenced in regular expressions by '\k' or '\g' as if they were > defined there. > > This could made regular expressions be much more readable, because one can build them based on smaller parts, > one can build special Libraries of regular expression parts that are usable in the applications, and one can > use regular expression parts that were build by others without complete understanding of their details. > > I think that this is worth to think about. > > Best regards, Wolfgang Hello Wolfgang, where is the difference to example = "(?a|b|c)" regex = /#{example}|nothing/ except that you make Regexp hold the example variable, and have a parse test on the regexp. And you may get these by something like this: bschroed@black:~/svn/projekte/ruby-things$ cat regexp.rb class Regexp def self.register(name, regexp) self.new(regexp.to_s) (@registered_res ||= {})[name] = regexp.to_s end def self.[](name) @registered_res[name] end end Regexp.register(:example, 'a|b|c') if /#{Regexp[:example]}|nothing/ =~ 'Well, that was just nothing' puts "Contains an example or nothing" end Regexp.register(:invalid, '(invalid(') bschroed@black:~/svn/projekte/ruby-things$ ruby regexp.rb Contains an example or nothing regexp.rb:4:in `initialize': premature end of regular expression: /(invalid(/ (RegexpError) from regexp.rb:4:in `new' from regexp.rb:4:in `register' from regexp.rb:19 So it seems a very specialized whish to me. Regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/