Tuesday, June 28, 2011

Rubying with Regexp

So far Ruby Koans is a lot of review for me. I learned a few new things (a lot about symbols!), but I finally get Regular expressions. This was almost completely new, but I run into the code quite often when looking through examples--most commonly at rubyquiz. Let's see if I can go over all the params from Koans...
Note: Ruby reads from left to right, so it'll return the first string to match from the left. There are some examples of this where you can check for all matches, so I noted those.

''[/text you want to match/] -- checks a string for matching content.
if what you are searching for isn't there, match comes up nil

''[/text you want to match?/] -- the question mark means the last character is optional. so [/oranged?/] checks for orange and oranged, but oranged is optional.

''[/text you want to match+/] -- the plus checks that last character to see if there are more, and will return all of the extra ones.

''[/text you want to match*/] -- asterisk is similar. it's like a combo meal of ? and +, so it returns all of them if that character exists in the string.

array.select { |a| a[/[characters you are checking as options]rest of string/] }
phew that was a long one. this replaces the first character with whatever is in the array. for example, [a/[bcfj]ar/] checks for bar, car, far, and jar.

''[/\d+/] returns all the digits as strings. similarly I could use [/[0-9]+/] as a range.

''[/\s+/] is for whitespace. It claims it's a shortcut, but I don't get that part yet. This is one I need to play around with a bit more.

''[/\w+/] is for words, which is anything A-z and 0-9.

''[/.+/] is for return carriages.

''[/^[characters negated]/] the ^ checks for everything but what's in the match string. Likewise, you can use capitals D, S, and W to negate any of the above checkers.

And for quickies...
[/\Atext/] /A anchors to the beginning, while \z anchors to the end (put \z at the end too!). Likewise, carat (^) and dollar sign ($) anchor to start and end of lines. \b anchors to a word boundary... another one I'd like to test a bit more.

.scan can be used to find all of the matches, and returns them in an array. Useful for finding all the words or numbers in a string.

.sub is used to replace the first, while .gsub is used to replace any time the string is matched.

Okay. That was a lot to take in. I'd like to start looking at more examples of Regexp and how it can be used in context, but in the meantime, back to Koans.

No comments:

Post a Comment