Всем привет. Мне нужно удалить последнюю запятую из строки. Я нашел regex, который выбирает как раз последнюю запятую, но в python оно, почему-то, убирает все запятые.
re.sub(r'(,)(?!.*,)', '', str)
regex
Было:
[
{ "key": "admin", "text": "Internal user" },
{ "key": "agent", "text": "Agent" },
{ "key": "corporate_landlord", "text": "Company Landlord" },
{ "key": "private_landlord", "text": "Landlord" },
{ "key": "tenant", "text": "Tenant" },
{ "key": "landlord", "text": "Landlord" },
{ "key": "none", "text": "None" },
]
Стало:
[
{ "key": "admin", "text": "Internal user" }
{ "key": "agent", "text": "Agent" }
{ "key": "corporate_landlord", "text": "Company Landlord" }
{ "key": "private_landlord", "text": "Landlord" }
{ "key": "tenant", "text": "Tenant" }
{ "key": "landlord", "text": "Landlord" }
{ "key": "none", "text": "None" }
]
Нужно:
[
{ "key": "admin", "text": "Internal user" },
{ "key": "agent", "text": "Agent" },
{ "key": "corporate_landlord", "text": "Company Landlord" },
{ "key": "private_landlord", "text": "Landlord" },
{ "key": "tenant", "text": "Tenant" },
{ "key": "landlord", "text": "Landlord" },
{ "key": "none", "text": "None" }
]
},' (возможно с пробелом) на
,`. Или запятую, после которой идет перевод строки]
. Почему просто не сделать замену видаre.sub(r",\s*\]", "]", subject)
?