0

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.

5
  • Your edit makes no sense. WHAT is the pattern you are looking for? If a regex is needed then you need to provide a description of what you are trying to match with that regex. This is possibly the most frustrating part of new user questions. We are not psychics, we can not read your mind. I'm not the one who down voted your question, but at this point it deserves to be. Commented Mar 24, 2015 at 16:49
  • I think my question is clearer now, could you answer it? Commented Mar 24, 2015 at 21:34
  • MUCH clearer. I think I can answer this, I'll take a look tonight when I have some free time. Commented Mar 24, 2015 at 23:14
  • OK, I get what you are trying to do but why are you using that regex? it's so overly complex. I'm editing my answer with something that will work. You will need to tell me if it fits your actual use case. Commented Mar 25, 2015 at 3:39
  • Because I'm a paranoia guy who thinks of malicious users everywhere. Not that this function should care about its input. Commented Mar 25, 2015 at 11:12

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Can I use x[pat[1]]? My example is indeed oversimplified (stated so), but so are all examples with regexes that do not ask to fix them.
Using the array notation on a string just returns that character in the string. So x[0] returns the first character. In your example you are using a regex to match every character. Why? If that is not what you want then you need to provide a less oversimplified example in the question or we can't tell what you want.
String#scan was what I needed, me thinks (checking now).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.