Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
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
Highlighted

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
Highlighted

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
Highlighted
ms
Super User ms
Super User

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);
Highlighted

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

Highlighted
drblove
Level III

Re: Regex to pull file from path with JSL

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

Article Labels

    There are no labels assigned to this post.