我的正则表达式需要能够找到字符串:
Visual Studio 2008
Visual Studio Express 2008
Visual Basic 2008
Visual Basic Express 2008
Visual C++ 2008
Visual C++ Express 2008
和许多其他类似的变体,用这一个单独的字符串替换
Visual Studio 2005
我试过"Visual(Basic | C++ | Studio)(Express)?2008",但它不起作用.有任何想法吗?
编辑:现在我尝试了"Visual(Basic)|(C++)|(Studio)(Express)?2008",但是替换后的行变为"Visual Studio 2005 Express 2008",用于输入"Visual Basic Express 2008".
它应该是
"Visual (Basic|C\+\+|Studio)( Express)? 2008" >>> import re >>> repl = 'Visual Studio 2005' >>> regexp = re.compile('Visual (Studio|Basic|C\+\+)( Express)? 2008') >>> test1 = 'Visual Studio 2008' >>> test2 = 'Visual Studio Express 2008' >>> test3 = 'Visual C++ Express 2008' >>> test4 = 'Visual C++ Express 1008' >>> re.sub(regexp,repl,test1) 'Visual Studio 2005' >>> re.sub(regexp,repl,test2) 'Visual Studio 2005' >>> re.sub(regexp,repl,test3) 'Visual Studio 2005' >>> re.sub(regexp,repl,test4) 'Visual C++ Express 1008'