简体   繁体   English

Python:删除括号序列及其中的内容(如果它们仅位于字符串的末尾)

[英]Python : Remove sequences of brackets and the content inside it if they are only at the end of a string

I want to remove brackets and what is inside only if they are at the end of the string.我想删除括号和里面的内容,只有当它们在字符串的末尾时。 Let's take three exemples :让我们举三个例子:

s1 = 'BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine[2][]'
s2 = "BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt['CHF']"
s3 = "BkToCstmrStmt/Stmt/Bal[1]/Amt['CHF']"

I want to get我想得到

s1 = 'BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine'
s2 = "BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt"
s3 = "BkToCstmrStmt/Stmt/Bal[1]/Amt"

Here is what I tried :这是我尝试过的:

name_parts = re.findall(r'[^\W_]+|[\W_]+', s3)

print(name_parts)

lenght = len(name_parts) - 1
    # we want to analize the last element of the list, if it contains '_-'
if lenght >= 0:  # it is to prevent an error if we have '' so an dimension '-1'
        # We do a loop while to test if the parts have '-_', if true we execute the loop
        # until it is false
    while re.match('[^A-Za-z/]', name_parts[lenght]) or re.match('[^A-Za-z/]', name_parts[lenght-1]) :
            # if it is true it will remove them
            name_parts[lenght] = ''  # it will remove them
            print(name_parts)
            lenght -= 1  # if the condition was true, we continue with one inferior part
else:
    pass

new_string = ''.join(map(str, name_parts))  # now that we have cleaned if it was necessary
    # we concatenate them

But It does not work.但它不起作用。 Anyone has an idea to efficiently do that ?任何人都有一个想法来有效地做到这一点?

Regex101 :正则表达式101

import re

s1 = 'BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine[2][]'
s2 = "BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt['CHF']"
s3 = "BkToCstmrStmt/Stmt/Bal[1]/Amt['CHF']"

for s in [s1, s2, s3]:
    s = re.sub(r'(\[[^]]*\])+$', '', s)
    print(s)

Prints:印刷:

BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine
BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt
BkToCstmrStmt/Stmt/Bal[1]/Amt

Here's a non-regex approach:这是一种非正则表达式方法:

s1 = 'BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine[2][]'
s2 = "BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt['CHF']"
s3 = "BkToCstmrStmt/Stmt/Bal[1]/Amt['CHF']"

for s in (s1, s2, s3):
    while s.endswith(']'):
        s = s[:s.rfind('[')]
    print(s)

Prints印刷

BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine
BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt
BkToCstmrStmt/Stmt/Bal[1]/Amt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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