-2

What would be the regular expression for such data

/home//Desktop/3A5F.py
path/sth/R67G.py
a/b/c/d/t/6UY7.py

i would like to get these

3A5F.py
R67G.py
6UY7.py
  • 1
    Hi, welcome to stackoverflow. What have you tried so far? (hint: no regular expressions are needed) – msvalkon Aug 05 '16 at 09:59
  • `"/home//Desktop/3A5F.py".split("/")[-1]` – msvalkon Aug 05 '16 at 10:01
  • Maybe a dupe of [*Python, extract file name from path, no matter what the os/path format*](http://stackoverflow.com/questions/8384737/python-extract-file-name-from-path-no-matter-what-the-os-path-format) – Wiktor Stribiżew Aug 05 '16 at 10:13

5 Answers5

2

It looks like you're parsing paths, in which case you should really be using os.path instead of regex:

from os.path import basename
basename('/home//Desktop/3A5F.py')
# 3A5F.py
tzaman
  • 44,771
  • 11
  • 88
  • 112
1

It is a simple split, no regex needed:

>>> "/home//Desktop/3A5F.py".split("/")[-1]
'3A5F.py'
Chris_Rands
  • 35,097
  • 12
  • 75
  • 106
1

As an alternative, you can get same result without regexps:

lines = ['/home//Desktop/3A5F.py', 'path/sth/R67G.py', 'a/b/c/d/t/6UY7.py']

result = [l.split('/')[-1] for l in lines]
print result
# ['3A5F.py', 'R67G.py', '6UY7.py']
Andriy Ivaneyko
  • 18,421
  • 4
  • 52
  • 73
0

use : [^\/]*\.py$

But this is a bad question. You need to show what you have try. Whe are not here to do your work for you.

baddger964
  • 1,260
  • 9
  • 17
0

You can use this.

pattern = ".*/(.*$)"
mystring = "/home//Desktop/3A5F.py"
re.findall(pattern, mystring)

You can also use os.path.split(mystring)

user2550098
  • 133
  • 1
  • 12