나는 괄호를 제거하고 문자열의 끝에있는 경우에만 내부에있는 것을 제거하고 싶습니다. 세 가지 예를 들어 보겠습니다.
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']"
난 갖길 원해
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"
내가 시도한 것은 다음과 같습니다.
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
그러나 작동하지 않습니다. 누구든지 효율적으로 할 아이디어가 있습니까?
Regex101 :
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)
인쇄물:
BkToCstmrStmt/Stmt/Ntry[2]/NtryDtls/TxDtls/RltdPties/Cdtr/PstlAdr/AdrLine
BkToCstmrStmt/Stmt/Ntry[2]/AmtDtls/InstdAmt/Amt
BkToCstmrStmt/Stmt/Bal[1]/Amt
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다