Regular expression help

Hello all

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

Does anyone have any ideas?

Thanks :slight_smile:

Here’s a quick version: Rubular: .*\/(\w+)\..*

That help?

Hi,

You don’t need a regular expression. If the file extension is always the
same (.int-APACHE.gz), you can use File#basename:

puts File.basename(‘c:/Documents and
Settings/Administrator/Desktop/version3/test_files/apserver2.int-APACHE.gz’,
‘.int-APACHE.gz’)

This returns the filename without the specified extension.

Hello everyone

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!

Kind Regards

Alex S. wrote in post #1056463:

File#basename would not work as they are dated and numbered on the live
network.

So? The method doesn’t actually look at the file, it only operates on
the filename string (like a regex would do).

You can also write

filename.split(’/’).last[/[^.]+/]

On Thu, Apr 12, 2012 at 4:21 AM, Alex S. [email protected]
wrote:

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.