Category Archives: Regular Expressions

Feature added to RubyCocoa Regular Expression Checker

Get the new app in the Box widget (to your right). This version lets you see the string(or substring) that was matched. I have not tested this extensively. The usual disclaimers apply.

Advertisements

A RubyCocoa Regular Expression Checker

Update: 6:45 p.m. GMT, As far as I know, you can just install the binary package of RubyCocoa and the default Apple installation of Ruby will be sufficient to make this app work.

Update: 6:19 p.m. GMT, I fixed a bug in the checker. Get the new one as usual from the Box widget.

Application icon credits: rox-ruby

I have uploaded a little Regular Expression checker I made in RubyCocoa(see RegEx.dmg in my Box widget to your left). I only have an Intel Mac to test it on, so I have no idea whether it will run correctly on PPC Macs. The code is licensed under the GPLv2. It may be the case the that you need RubyCocoa properly installed to run this app. See my post on installing BioRuby for Mac OS X for a link to Dan Benjamin’s instructions on updating your Ruby installation(adapt these instructions accordingly for Ruby 1.8.5). For installing RubyCocoa under Ruby 1.8.5, if memory serves, these commands (once you extract the files from the archive and go to the to-level directory of RubyCocoa),

$ ruby install.rb –help # print all options

$ ruby install.rb config

$ ruby install.rb setup

from this website are the key instructions. You may have to prefix sudo in one of the above three commands.

Screenshot:

Screenshot


			    NO WARRANTY
  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.

Using Ruby to skim log files

Say we have a bunch of log files, whose last line is the main thing you care about(you might imagine it has a distinct prefix like “ANSWER”–I know, that’s contrived :p The following code assumes the script is in the same directory as the log files. It gets all the files ending in .txt, opens them, puts the name of the file to the console, and then puts the result line on the console. We could keep appending to a file, but for a “throwaway” script, we can just make do with: ./process.rb > results.txt(OS X, assuming chmod +x process.rb was applied), or ruby results.txt > process.rb (on Windows).

#!/usr/bin/env ruby

file_list = Array.new
file_list = Dir[“*.txt“]
file_list.each do |file|
File.open(file,”r“) do|f_object|
print(file.to_s + “ “)
f_object.each_line {|line| puts line if line =~ /ANSWER/ }
end
end

Comments on Regular Expressions

N.B. This post is adapted from my general blog

I really like regular expressions. This real-world example shows you why:

I have this C++ test code generated by a script written by someone else. Due to a minor bug in the script, it generates invalid code like if (blah != ) , where that space should have been 0. Now there are about 500 instances of these. For this time, I can get away with using the search/replace feature in Visual Studio, Xcode, or similar editors. But what if there were inconsistent copies of if(blah!=) where each copy had different spacing levels between the if and opening parens, and = and closing parens, etc?

You can use regular expressions to do a search replace:

(you can try other editors too, possibly even Visual Studio, Xcode, etc.)

In Vim, in command mode

:%s/if\s*(blah\s*!=\s*)/if (blah != 0)/gc

translation: %s -> command for search replace
\s* stands for 0 or more spaces
the first set of /…/ delimit the string we are looking for
the second set of /…/ delimit what we want to replace the string with
the gc at the end tell vim to ask us before replacing each instance.

So, you see, regular expressions can really save you time.