julka
julka

Reputation: 1212

Ruby regex replace some subpattern captures

I have regex for path parsing. Below is the part of regex that repeats multiple times.

dir_pattern = /
    \/?
    (?<dir>  #pattern to catch directory
        [^[:cntrl:]\/\n\r]+  #directory name
    )
    (?=\/)  #indistinguishable from file otherwise
/x  

Input:

/really/long/absolute/path/to/file.extension

Desired output:

to/really/long/file.extension

I want to cut off some (not all directories) and reorder remaining ones. How could I achieve that?

Since I'm already using regexes for filtering files needed, I would like to keep using them.

Upvotes: 0

Views: 144

Answers (1)

Beartech
Beartech

Reputation: 6411

Ok, here is a regex answer based on the new information posted above:

rx = /\/[^\/]+/i
    # matches each character that is not a '/' 
    # this ensures any character like a '.' in a file name or the dot
    # in the extension is kept.
path = '/really/long/absolute/path/to/file.extension'

d = path.scan(rx)
   # returns an array of all matches ["/really", "/long", "/absolute", "/path", "/to", "/file.extension"]
new_path = [y[4], y[0], y[1], y[-1]].join
   # returns "to/really/long/file.extension"

Lets wrap it in a method:

def short_path(path, keepers)
  rx = /\/[^\/]+/i
  d = path.scan(rx)
  new_path = []
  keepers.each do |dir| 
    new_path << d[dir]
  end
  new_path << d[-1]
  new_path.join
end

Usage: just past the method the path and an array of the positions you want to keep in the new order.

path = '/really/long/absolute/path/to/file.extension'
new_path = short_path(path, [4,0,1])
 # returns '/to/really/long/file.extension'

If you need to remove the first '/' for a relative path just:

new_path.sub!(/\//, '')

Old answer using string manipulation without regex...

x = "01234567 capture me!"
puts "#{x[7]}#{x[4]}#{x2}"

#=> "742"

Upvotes: 1

Related Questions