Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract a string after a number of forward slashes from inside a string

Hy have a string which corresponds to a file location in linux, i want to extract just the part of it that corresponds to the username and that always comes after the 6th forward slash and then store it in a new string variable:

str1 = "/usr/share/nginx/website/orders/jim/17jkyu.xlsx"
n = 0
user = ','

for ch in str1:
    if ch == '/':
        n += 1
    if n == 6:
        user.join(ch)
    if n == 7:
        break

print(user + '\n')

In this case i want to extract jim and put it into the string user.
So far is not working

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Split the string then pull the index value.

str1 = "/usr/share/nginx/website/orders/jim/17jkyu.xlsx"
user = str1.split('/')[6]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading