-1

I need to find the substring after the last \ in the sting (path to file, windows). I suppose that an elegant pythonic way should be possible (without counting "\" or writing a regression method).

str1 = 'qwerty\\asd\\zxc\x\\c'
str2 = 'zz\\z\\x\\c\\v\\b\\n\\m\\m2m\\m3m'

How to edit this line of code?

found_name = re.findall(r'\\(.*?)', mystr)

Currently it returns all after the fist back slash.

cat_on_the_mat
  • 100
  • 1
  • 9

1 Answers1

1

This doesn't require a regex, just use split:

>> str1 = 'qwerty\\asd\\zxc\x\\c'
>> str1.split(r'\\')[-1]
'c'

If you have to use regex for some reason then use:

>>> re.findall(r'.*\\(.*)$', str1)
['c']
anubhava
  • 761,203
  • 64
  • 569
  • 643