Split Regex "\" doesn't work

I am trying to split out the filename from a file path.

If the file path is:

D:\General\Manuals\Omron\PLC\NJ_NX_Motion Control User Manual_W507-E1-11.pdf

I want to extract the filename:

NJ_NX_Motion Control User Manual_W507-E1-11.pdf

I thought I could use something like:

split({Root Container.FilePath}, '\')[5,0]

But i’m getting the error:

Where Char 43 is the end of the expression.

Is the “” character one of those funny characters which cannot be used directly? Do you need to use some other syntax like “\” or “”""?


Yes a backslash needs to be doubled.
So just do

s = {Root Container.FilePath}
print s.rsplit("\\",1)

If you’re just looking to extract the filename, you can use rfind() to find the rightmost occurrence. This way, you can also easily keep the path, if you so choose.

[code]fullPath = ‘D:\General\Manuals\Omron\PLC\NJ_NX_Motion Control User Manual_W507-E1-11.pdf’

splitPoint = fullPath.rfind(’\’)

path = fullPath[:splitPoint+1]
fileName = fullPath[splitPoint+1:]

print path
print fileName [/code]

Not only do you have to double the backslash in a quoted string, in a regex to have it as a normal character, you have to double it again. In python, you can use the raw string syntax to avoid interpreting backslashes (the first doubling). The expression system doesn’t have that option. So, in an expression:split({Root Container.FilePath}, '\\\\')[5,0]

Ah… the ol double down. :stuck_out_tongue:

I tried “”, “\” and “\”, but who would have thought to do “\\”!

Awesome thanks for the replies everyone.