proutils edited this page Sep 14, 2010 · 7 revisions
Clone this wiki locally

Yea, I know what you are going to say. “I can do that with ___” Fill in the blank with grep, awk, sed, perl, etc. But honestly, none of these tools are “Language 2.0” (read “post-Ruby”). What I want is a simple command-line tool that gives me quick access to a regular expression engine. No more, no less.

Now I could have written this in Perl. I’m sure it would be just as good, if not better since Perl’s regular expression engine rocks, or so I hear. But Ruby’s is pretty good too, and getting better (with 1.9+). And since I know Ruby very well. Well that’s what you get.


Okay, check it out. It’s real simple. Supply a regular expression and a file to match against to the regex command.

  $ regex '=begin.*?\n(.*)\n=end' sample.rb

It does exactly what you would expect. It returns the content between the first =begin…=end clause it comes across. To see all such comments, you can use the --repeat option.

  $ regex --repeat '=begin.*?\n(.*)\n=end' sample.rb

By default regex produces string output. Regular expression groups are delimited by ASCII 29 (035 1D) END OF GROUP, and repeat matches are delimited by ASCII character 30 (036 1E) END OF RECORD.

  $ regex '=begin.*?\n(.*)\n=end' sample.rb

Instead of string output, regex also supports YAML and JSON output.

  $ regex --yaml --repeat '=begin.*?\n(.*)\n=end' sample.rb

In this case the returned matches are delimited using an array of arrays.

Check out the --help and I am sure the rest will come to you easily enough.


This is a very early release. So don’t expect every feature under the sun just yet, or that every detail is going to work peachy keen. But hey, if something needs fixing or a feature needs adding, well then get in there and send me a patch. Open source software is built on TEAM WORK, baby.

And expect a potential for rapid change here at the beginning.