cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Don’t miss your chance to experience Discovery Summit Europe at our best available rate! Early bird registration through 31 Oct.
  • Image to Table JMP extension imports single or batch images as data tables. Useful to extract curves from pictures or convert image data to curve data to use in analyses like Functional Data Explorer. Download and install the extension.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
drblove
Level III

Regex to pull file from path with JSL

I am sure this is a straightforward answer to this question, but if I have the following:

 

full_path[i] = "C:\dir1\dir2\dir3\dir4\file.xlsx"

and I want to get just "file.xlsx" and the directory depth is variabile, i.e. maybe 2 directories maybe 5, how would I do it?  In Perl I would split the string into an array by the \ and grab the last part of the array, but not sure how to do this in JSL.  Any help would be appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Regex to pull file from path with JSL

word ( -1, full_path[i], "\" )

 

You can use negative values, in the word() function, so -1 gives the last word, using the delimiter of choice.

 

 

View solution in original post

5 REPLIES 5

Re: Regex to pull file from path with JSL

You can use the JSL function Words() to separate a path into its component part, and then take just the last piece. Example:

 

full_path = {"C:\dir1\dir2\dir3\dir4\file.xlsx"}; i = 1; // setup

full_path[i] = "C:\dir1\dir2\dir3\dir4\file.xlsx";

filename = Words(full_path[i], "\"); // separate the path based on the backslash as a delimiter - returns a list

filename = filename[N Items(filename)]; // get the last string in the list

 

/*:

"file.xlsx"

Jeff_Perkinson
Community Manager Community Manager

Re: Regex to pull file from path with JSL

Try the Words() function. 

 

full_path = "C:\dir1\dir2\dir3\dir4\file.xlsx";

listofwords=words(full_path, "\");

filename=listofwords[nitems(listofwords)];

/*:

"file.xlsx"

 

-Jeff
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Regex to pull file from path with JSL

Two examples of how it could be done:

full_path = "C:\dir1\dir2\dir3\dir4\file.xlsx";

//Regex (fails if filename contains non-word characters)
file1 = Regex(full_path, "[\w.]+$"); 
// Split string
file2 = Reverse(Words(full_path, "\"))[1];

Show(file1, file2);

Re: Regex to pull file from path with JSL

word ( -1, full_path[i], "\" )

 

You can use negative values, in the word() function, so -1 gives the last word, using the delimiter of choice.

 

 

drblove
Level III

Re: Regex to pull file from path with JSL

This is a great solution, one line.  Thank you very much!

Recommended Articles