Regex to drop filename from path

OK I cry uncle after trying to get a working solution for 2+hours.

What I’m trying to do is get to the same folder level as where a zip file resides. This worked fine because there is a standard format to the filename… Well up until today! So my solution stopped working. I was using a regex replace in string manipulation to match and remove the filename. It seems the standard is no longer being kept so I need to be smarter. I know I can break apart the path and reconstruct to the level I need using cell splitter and combiner, but I’m betting there is a smarter way.

Matter of fact I thought I had found the perfect regex…([^\]+$) This works in regex101.com but when I use it in String manipulation it matches my whole path and wipes it out. I’m hoping someone can help me with this. I’ve attached an example to play with.

Thanks for taking a look!
Jason
Drop Filename Regex.knwf (6.1 KB)

Drop Filename Regex.knwf (18.8 KB)

This shows regex and a cool trick to split via URL to File Path node. You were almost there, KNIME just requires more escape characters so that the slash isn’t read as a regex command then Regex 101.

4 Likes

Hi @j_ochoada

After you have created the regex on regex101, click on Code Generator, Select Java and copy the content of the final String regex. That version is compatible with KNIME which, as @iCFO mentioned, contains the correct number of backslashes.

4 Likes

For completeness the column expression provides path functions which should also allow you to extract the path without file


br

2 Likes

@iCFO @ArjenEX and @Daniel_Weikert

Thank you all for all the quick help! So very appreciated!

Jason

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.