I have written a script to search the current directories and grab the
file path for a set of filtered files. Example of what it returns:
c:/Documents and
Settings/Administrator/Desktop/version3/test_files/apserver1.int-APACHE.gz
c:/Documents and
Settings/Administrator/Desktop/version3/test_files/apserver2.int-APACHE.gz
c:/Documents and
Settings/Administrator/Desktop/version3/test_files/apserver3.int-APACHE.gz
What I am trying to do is extract only the server names. So apserver1,
apserver2 and apserver3. I need a regular expression that will grab
everything after the last forward slash and then the text before the
first “.” So far I can only get it grab everything after the first
forward slash. So far I have:
apache_server_filter.each do |x|
puts x.to_s.scan(/(/\D+)/)
end
This pulls out: /Documents and Settings/Administrator/Desktop/version
/test_files/ap
File#basename would not work as they are dated and numbered on the live
network. I could have used that to save me time on some old scripts
though >( I got it working using Jeffs regex link. That site has been
bookmarked. Thanks everyone as usual for the fast replies and helpful
info!
What I am trying to do is extract only the server names. So apserver1,
apserver2 and apserver3. I need a regular expression that will grab
everything after the last forward slash and then the text before the
first “.”
First, I’d use File#basename to get the filename. Then you can use the
String#[] method with a regex that gets everything up to the first
point, like this:
1.9.2p290 :009 > s = “c:/Documents and
Settings/Administrator/Desktop/version3/test_files/apserver1.int-APACHE.gz”
=> “c:/Documents and
Settings/Administrator/Desktop/version3/test_files/apserver1.int-APACHE.gz”
1.9.2p290 :010 > File.basename(s)[/[^.]*/]
=> “apserver1”
Jesus.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.