So yeah came across this while browsing why's site. Very cool. I hadn't been put into a situation where I needed to match the same thing on multiple lines (what with the nature of directory based information). But still this is damn cool. Give a regular expression to a string#scan and get back all the matching elements.
Means I can slurp in an entire file and pick out the bits I want. Memory overhead goes down the drain, but still for quick snatch-and-grabs, that has be the coolest thing.
re = /pattern./
STDIN.readlines.join("").scan(re)
too cool.
Tweets
Thursday, August 11, 2005
Subscribe to:
Post Comments (Atom)
1 comment:
If you don't want to load the entire file and scan, you can use IO.grep:
STDIN.grep( /pattern./ ) do |line|
puts line
end
Prints out each line which has a match. Doesn't work for multiline searches, but it's another option. You're right, though, scan is a good one.
Thanks for reading! Beaver tales!
Post a Comment