正则表达式替换

时间:2011-09-09 20:42:17

标签: python regex

我有一个像这样的字符串:

{{foobar
| option1 = foo
| option2 = foo
| <!-- more options -->
| something = xxx
| thisoption = xxx
| <!-- more options -->
}}

我希望将“thisoption = xxx”替换为“thisoption = abc”。它不应该是以下几点:

{{foobar
| option1 = foo
| option2 = foo
| thisoption = xyz
}}

{{foobar
| option1 = foo
| option2 = foo
}}
{{someotherthing
| thisoption = xxx
}}

我该怎么做?

4 个答案:

答案 0 :(得分:2)

这个问题有点模糊,但试试这个:

re.sub(r'(?P<name>foobar[^}]* = xxx\b)', r'\g<name>2', string)

它会查找foobar的匹配项和xxx的匹配项,中间没有任何结束括号,即xxx位于foobar

>>> string = '{{foobar\n| option1 = foo\n| option2 = foo\n| <!-- more options -->\n| thisoption = xxx\n| <!-- more options -->\n}}'
>>> re.sub(r'(?P<name>foobar[^\}]*xxx)', r'\g<name>2', string)
'{{foobar\n| option1 = foo\n| option2 = foo\n| <!-- more options -->\n| thisoption = xxx2\n| <!-- more options -->\n}}'

您的第一个测试用例;在这种情况下,xxx被xxx2替换。

>>> string = '{{foobar\n| option1 = foo\n| option2 = foo\n| thisoption = xyz\n}}'
>>> re.sub(r'(?P<name>foobar[^\}]*xxx)', r'\g<name>2', string)
'{{foobar\n| option1 = foo\n| option2 = foo\n| thisoption = xyz\n}}'

第二个测试案例;没有任何反应。

>>> string = '{{foobar\n| option1 = foo\n| option2 = foo\n}}\n{{someotherthing\n| thisoption = xxx\n}}'
>>> re.sub(r'(?P<name>foobar[^\}]*xxx)', r'\g<name>2', string)
'{{foobar\n| option1 = foo\n| option2 = foo\n}}\n{{someotherthing\n| thisoption = xxx\n}}'

第三个测试用例;没有任何反应。

希望这有帮助!

答案 1 :(得分:0)

要回答我自己的问题,这似乎可以解决问题:

re.sub(r"(\{\{foobar[^\}]*)thisoption ?= ?xxx", r"\1thisoption = abc", string)

感谢所有帮助过的人!

答案 2 :(得分:0)

您的示例看起来有点像INI设置文件,只是使用不同的语法。你应该查看python的ConfigParser模块,它已经处理了所有这些。

答案 3 :(得分:0)

import re

ss = '''{{foobar
| option1 = foo
| option2 = foo
| <!-- more options -->
| something = xxx
| thisoption = xxx
| <!-- more options -->
}}'''

tt = '''{{foobar
| option1 = foo
| option2 = foo
| thisoption = xyz
}}'''

uu = '''{{foobar
| option1 = foo
| option2 = foo
}}
{{someotherthing
| thisoption = xxx
}}'''


regx = re.compile('(\{\{foobar *\r?\n'
                  '([^}]*\r?\n)+?'
                  '\| thisoption *= *)(xxx)')


for x in (ss,tt,uu,vv,zz):
    print regx.sub('\\1abcd',x)
    print
    print re.sub(r'(?P<name>foobar[^\}]*xxx)', r'\g<name>2', x)
    print '\n-----------------------------------------------------------'

结果

{{foobar
| option1 = foo
| option2 = foo
| <!-- more options -->
| something = xxx
| thisoption = abcd
| <!-- more options -->
}}

{{foobar
| option1 = foo
| option2 = foo
| <!-- more options -->
| something = xxx
| thisoption = xxx2
| <!-- more options -->
}}

-----------------------------------------------------------
{{foobar
| option1 = foo
| option2 = foo
| thisoption = xyz
}}

{{foobar
| option1 = foo
| option2 = foo
| thisoption = xyz
}}

-----------------------------------------------------------
{{foobar
| option1 = foo
| option2 = foo
}}
{{someotherthing
| thisoption = xxx
}}

{{foobar
| option1 = foo
| option2 = foo
}}
{{someotherthing
| thisoption = xxx
}}

-----------------------------------------------------------

目前我所看到的 discipulus 的解决方案在以下情况下不起作用:

vv = '''{{foobar
| option1 = foo
| option2 = foo
| thisoption = xxx
| last_xxxxx_option = toxic
}}'''

zz = '''{{foobar
| option1 = foo
| option2 = foo
| thisoption = xxx
| last_option = toxxxic
}}'''

为:

{{foobar
| option1 = foo
| option2 = foo
| thisoption = abcd
| last_xxxxx_option = toxic
}}

{{foobar
| option1 = foo
| option2 = foo
| thisoption = xxx
| last_xxxxx2_option = toxic
}}

-----------------------------------------------------------
{{foobar
| option1 = foo
| option2 = foo
| thisoption = abcd
| last_option = toxxxic
}}

{{foobar
| option1 = foo
| option2 = foo
| thisoption = xxx
| last_option = toxxx2ic
}}

-----------------------------------------------------------