Home > database >  Python regexp to get substring contains '/\'
Python regexp to get substring contains '/\'

Time:11-20

I have string

ss='/users/parun/kk/jdk/bin/\x1b[01;31m\x1b[kjava\x1b[m\x1b[k'

How to get output

'/users/parun/kk/jdk/bin' only from the above

I tried

import re
re.split(r'\/\\')

But not working

CodePudding user response:

A regex search might be the best option here:

ss = '/users/parun/kk/jdk/bin/\x1b[01;31m\x1b[kjava\x1b[m\x1b[k'
path = re.findall(r'^.*/jdk/bin', ss)[0]
print(path)  # /users/parun/kk/jdk/bin
  • Related