Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Tuesday, March 23, 2010

Matching Numeric Ranges with Ruby

regular-expressions.info have dedicated a page to how this can be accomplished using regular expressions alone. This approach is limited in the ranges that can be matched and the patterns add to the complexity of the expression needed to match desired lines.

Realizing this shortcoming of a pure regex approach, I decided to follow a programmatic one with ruby. Here is a command that will display Java VM (run with -verbose:gc) Garbage Cleaning output with a pause time of between 2 and 5 seconds.

cat ./logs/catalina.out | ruby -e 'STDIN.each {|line| puts line if line =~ /\[GC [0-9]+K->[0-9]+K\([0-9]+K\), ([0-9]+\.[0-9]+) secs\]/ and (2..5).include?($1.to_f)}'
Ruby receives a standard input a line at a time and checks it against the regular expression. If a line matches, the part of the pattern enclosed by un-escaped parentheses is made available in the variable $1. That variable is converted to float and compared to the given range. The line is then printed to standard output if the value falls within the range.

Tuesday, December 1, 2009

Regex Repetition Matching

I use regex on and off. A situation arose where I needed to use repetition matching, something I knew existed but had never used before.

I needed to match a string with at most 36 characters. Using the regex below worked but it also matched strings with commas inside them
.{,36}
I specified that I did not wish to have commas in the string, resulting in this next expression.
[^,].{,36}
After this change, the expression stopped matching the target strings e.g. 9F7096D0D20949ACB2DA1EE57488F015
or
99AD7ECC-222A-AC8A-4BF8-F04B159DDBFB
It instead matched a string like
9200','LI','NA',null,'Y','L','N','N',

Realizing that the attempt to leave out the commas had caused the trouble, I adjusted the expression, which matched the desired strings.
[^,]{,36}