I want to get only the file name with extension from the path:
C:\\\\Users\\\\anandada\\\\workspace\\\\MyTestProject\\\\
It is because you don't use a raw string. The double backslash is interpreted as an escape for the closing square bracket. You need to write:
fileName = re.match(r"[^\\]*.c$", fileName)
with the raw string format \\
is seen as a literal backslash as expected.
You need to double the doubled escapes again, or use a raw string instead:
fileName = re.match("[^\\\\]*.c$",fileName)
or
fileName = re.match(r"[^\\]*.c$",fileName)
otherwise first Python, then the regular expression compiler will interpret those backslashes, resulting in the ]
being escaped:
>>> print("[^\\]*.c$")
'[^\]*.c$'
Also see the Blackslash Plague section of the Python Regex HOWTO.
Next, you need to be aware that re.match
anchors to the start of the string. You'll probably want to use re.search()
instead here. See the match() vs. search() section:
The
match()
function only checks if the RE matches at the beginning of the string whilesearch()
will scan forward through the string for a match. It’s important to keep this distinction in mind.
You may also want to escape the .
in the .c
part; .
matches any character, so foobaric
would also match; the i
would satisfy the .
pattern.
The re.match()
and re.search()
functions return a match object, not the matched part of the string. You'll have to extract that part explicitly:
fileName = re.search(r'[^\\]*\.c$', fileName).group()
Demo:
>>> import re
>>> fileName = 'C:\\Users\\anandada\\workspace\\MyTestProject\\src\\OpenTest.c'
>>> re.search(r'[^\\]*\.c$', fileName).group()
'OpenTest.c'