简体   繁体   中英

how to search a combination od dot and forward slash in python

I am beginner in Python and I want to replace some specific characters from my string by regex.

HR_list = '../frankfurt/', '/CA/en/vancouver', '../../../IT/en/rom/'

If my string includes '../' I want to replace it with DE/en, and if my string includes '../../../' I wan o replace it with " ". So my code is:

for href in HR_list:
    if (re.findall('\\b\.\.\/\.\.\/\.\.\\b', href)):
        LIST.append("mystring" + (href.replace("../../..", "")))
    elif (re.findall('\\b\.\.\\b', href)):
        LIST.append("mystring" + (href.replace("..", "/DE/en")))
    else:
        LIST.append("mystring" + href)

The expected result should be:

mystring/DE/en/frankfurt, mystring/CA/en/vancouver, mystring/IT/en/rom/

How about using replace twice:

HR_list = '../frankfurt/', '/CA/en/vancouver', '../../../IT/en/rom/'

['mystring' + href.replace('../../..', '').replace('..', '/DE/en') for href in HR_list]
# ['mystring/DE/en/frankfurt/', 'mystring/CA/en/vancouver', 'mystring/IT/en/rom/']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM