更改大括号内的字符串(选项以|分隔)

时间:2018-02-13 20:38:08

标签: python regex

我正在尝试更改以下字符串中的花括号之间的文本:

s = "As soon as {female_character:Aurelia|Aurelius} turned around the corner, {female_character:she|he} remembered that it was the wrong way and would eventually end in a cul-de-sac. Spinning around, {female_character:Aurelia|Aurelius} tried to run back out, but the way was already blocked by the vendor. In this dark alley way, nobody would see or care what happened to some poor beggar turned thief. Should {female_character:Aurelia|Aurelius} put up a fight in hopes of lasting long enough to escape or give up now and trust to the mercy of the vendor?"

我的问题是,如何将Python逻辑应用于该字符串中的文本,以便文本中的{female_character:Aurelia | Aurelius}应用以下逻辑:

if (whatever is on the left side of the colon) == True:
    (replace {female_character:Aurelia|Aurelius} with the option on the left side of the |) 
else:
    (replace {female_character:Aurelia|Aurelius} with the option on the right side of the |) 

还有其他几点需要注意,字符串是从json文件中提取的,并且会有许多类似的文本。另外,有些大括号内有括号如下:{strong_character:对于他的年龄来说很大| {small_character:虽然他的年龄很小,但他是一个非常快的战士|虽然平均大小,是一个熟练的战士}}

我确信任何人都可以告诉我,我仍然是编码新手并且正在尝试学习Python。所以我提前为任何无知而道歉。

1 个答案:

答案 0 :(得分:0)

您可以使用regular expression来查找变量及其文本替换。正则表达式支持分组,因此您可以在单独的组中同时抓取TrueFalse,然后根据找到的变量的当前值替换整个匹配正确的小组。

但是,对于嵌套表达式,它会变得更难。最好是以这样的方式构造正则表达式,使其与 outer 嵌套级别不匹配。第一次,内部支撑表达式将被纯文本替换,然后第二个循环将匹配并更改其余部分。

因此可能需要多个替换循环,但有多少呢?这取决于嵌套支撑的数量。您可以将循环设置为“足够大”的数字,例如10,但这有几个缺点。例如,你需要确保你不小心嵌套超过10次;如果你的句子只有一个级别的大括号​​而且没有嵌套,它仍然会循环9次以上,什么也不做。

解决此问题的一种方法是计算嵌套大括号的数量。我认为我的findall正则表达式正确地做到了这一点,但我可能错了。

import re

def replaceVars(vars,text):
    for loop in range(len(re.findall(r'\{[^{}]*(?=\{)', text))+1):
        for var in vars:
            if vars[var]:
                text = re.sub ('\{'+var+r':([^|{}]+)\|([^|{}]+?)\}', r'\1', text)
            else:
                text = re.sub ('\{'+var+r':([^|{}]+)\|([^|{}]+?)\}', r'\2', text)
    return text

s = "As soon as {female_character:Aurelia|Aurelius} turned around the corner, {female_character:she|he} remembered that it was the wrong way and would eventually end in a cul-de-sac. Spinning around, {female_character:Aurelia|Aurelius} tried to run back out, but the way was already blocked by the vendor. In this dark alley way, nobody would see or care what happened to some poor beggar turned thief. Should {female_character:Aurelia|Aurelius} put up a fight in hopes of lasting long enough to escape or give up now and trust to the mercy of the vendor? Puppy {strong_character:is big for his age|{small_character:although small for his age, is a very quick warrior|although average size, {female_character:she|he} is a skilled warrior}}"
variables = {"female_character":True, "strong_character":False, "small_character":False}

t = replaceVars(variables,s)
print (t)

结果

  

当Aurelia转过身来时,她记得这是错误的方式,并最终以一条死胡同结束。转过身来,Aurelia试图跑回去,但这种方式已被供应商阻止。在这条黑暗的小巷里,没有人会看到或关心一些可怜的乞丐变成了小偷。 Aurelia是否应该进行一场战斗,希望能够持续足够长的时间以逃避或放弃现在并信任供应商的怜悯?小狗虽然平均身材,但她是一名熟练的战士