-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
  • 46,925
  • 11
  • 90
  • 115
1

It is a simple split, no regex needed:

>>> "/home//Desktop/3A5F.py".split("/")[-1]
'3A5F.py'
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
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
  • 20,639
  • 6
  • 60
  • 82
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,199
  • 9
  • 18
0

You can use this.

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

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

user2550098
  • 163
  • 1
  • 13