Brian Candler
2/7/2007 4:57:00 PM
> def find_share(sharelist, match)
> re = /^#{Regexp.escape(match)}$/
> sharelist.split("\n\n").find { |s| re.match(s) }
> end
>
> puts find_share(sharelist, "\\\\testserver2\\BACKUPTEST")
Or more cryptically, but much faster as it's a single regexp scan and no
intermediate array is created:
def find_share(sharelist, match)
return $1 if /^#{Regexp.escape(match)}$\n(.*?\n)\n/m =~ sharelist + "\n"
nil
end
puts find_share(sharelist, "\\\\testserver2\\BACKUPTEST")
Note that ^ and $ match start and end of line anywhere within the string,
not just the start and end of the string.
Here find_share() also strips off the sharename, leaving just the users and
their rights, but you can change this by moving the capture parentheses in
the regexp if you wish.
B.