Вопрос по string, python, regex – Заменить строку, расположенную между

12

Вот моя проблема: в переменной, которая является текстовой и содержит запятые, я пытаюсь удалить только запятые, расположенные между двумя строками (на самом деле[ а также]). Например, используя следующую строку:

input =  "The sun shines, that's fine [not, for, everyone] and if it rains, it Will Be better."
output = "The sun shines, that's fine [not for everyone] and if it rains, it Will Be better."

Я умею пользоваться.replace для всей переменной, но я не могу сделать это для части этого. На этом сайте есть несколько тем, но мне не удалось использовать их для собственного вопроса, например:

Repeatedly extract a line between two delimiters in a text file, Python Python finding substring between certain characters using regex and replace() replace string between two quotes
Error: User Rate Limit Exceeded Ashwini Chaudhary

Ваш Ответ

4   ответа
3

Error: User Rate Limit Exceeded

,(?=[^][]*\])

Error: User Rate Limit Exceeded

re.sub(r",(?=[^][]*\])", "", str)
Error: User Rate Limit Exceeded
0

Error: User Rate Limit Exceeded[]Error: User Rate Limit Exceeded[/Error: User Rate Limit Exceeded/]Error: User Rate Limit ExceededsplitError: User Rate Limit Exceeded/Error: User Rate Limit ExceededoddError: User Rate Limit ExceededcommaError: User Rate Limit Exceeded

>>> Variable = "The sun shines, that's fine [not, for, everyone] and if it rains,
                it Will Be better."
>>> chunks = Variable.replace('[','[/').replace(']','/]').split('/')
>>> ''.join(sen.replace(',','') if i%2 else sen for i, sen in enumerate(chunks))
"The sun shines, that's fine [not for everyone] and if it rains, it Will Be 
 better."
Error: User Rate Limit Exceeded user1453786
18
import re
Variable = "The sun shines, that's fine [not, for, everyone] and if it rains, it Will Be better."
Variable1 = re.sub("\[[^]]*\]", lambda x:x.group(0).replace(',',''), Variable)

Error: User Rate Limit Exceededre.subError: User Rate Limit Exceeded

Error: User Rate Limit Exceededvar1 = re.sub("re", fun, var)Error: User Rate Limit ExceededvarError: User Rate Limit Exceeded"re"Error: User Rate Limit ExceededfunError: User Rate Limit Exceededvar1Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded[ (\[Error: User Rate Limit Exceeded] ([^]]*Error: User Rate Limit Exceeded] (\]Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

lambda x: group(0).replace(',', '')

Error: User Rate Limit Exceededgroup(0)Error: User Rate Limit Exceeded','Error: User Rate Limit Exceeded''Error: User Rate Limit Exceeded,Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded user1453786
Error: User Rate Limit Exceeded"not, for, everyone] and if it rains, it Will [a,c],"Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededsubError: User Rate Limit Exceeded
-2

Error: User Rate Limit Exceeded

sentence = "the quick, brown [fox, jumped , over] the lazy dog"
left, bracket, rest = sentence.partition("[")
block, bracket, right = rest.partition("]")

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

new_sentence = left + "[" + block.replace(",","") + "]" + right
print new_sentence # the quick, brown [fox jumped over] the lazy dog

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededstringError: User Rate Limit Exceededstring moduleError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded.split('[', 1)Error: User Rate Limit Exceeded.partitionError: User Rate Limit ExceededrestError: User Rate Limit ExceededstringError: User Rate Limit ExceededstringError: User Rate Limit Exceeded"the quick, brown [the quick brown [fox jumped over] the lazy dog".
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded"a,b [c,d] e,f [g,e] h,i")

Похожие вопросы