Search code examples
rubyregexstringpathname

How to extract path from a string in ruby (between 1st and last fwd slash inclusive)


I have been writing a ruby script that goes through a text file and locates all lines that begin with output path and stores it into a string (linefromtextfile) for that line. So typically it locates lines as below

"output_path":"/data/server/output/1/test_file.txt","text":
"output_path":"/data/server/output/2/test_file.txt","text":

And I want to extract from the lines the pathname (pathtokeep) only and write out to a file, i.e:

/data/server/output/1/
/data/server/output/2/

I have tried this RegEx but its not working:

pathtokeep=linefromtextfile.split(?:\$/.*?/)([^/]*?\.\S*)

Please someone advise here on my RegEx - is split the right way to go or is there an easier way to do this?


Solution

  • If your file has the always the same structure you could do it without a regex too.

    line = '"output_path":"/data/server/output/1/test_file.txt","text":'
    
    path = line.split(/:"|",/)[1]
    # => "/data/server/output/1/test_file.txt"
    
    basename = File.basename(path)
    # => "test_file.txt"
    
    File.dirname(path) + '/'
    # => "/data/server/output/1/"