Skip to main content
 首页 » 编程设计

python之如何在不声明内容变量的情况下指定以 "testing"开头的 python 正则表达式

2025年05月04日30powertoolsteam

我编写了以下 python 代码,用于在 Windows 中卸载“testing_kip-win32”软件。

import wmi 
import re 
 
 c = wmi.WMI() 
 
print ("Searching for matching products...") 
for product in c.Win32_Product(Name = "testing_kip-win32"): 
  print ("Uninstalling" + product.Name + "...") 
  result = product.Uninstall() 

但是在上面的代码中,我没有给出全名“testing_kip-win32”,而是给出了一个以“testing”开头的软件名称。那么这个脚本应该卸载“testing_kip-win32”。

有什么想法吗?

提前致谢

注意:我使用的是 python 2.7

请您参考如下方法:

您可以使用正则表达式模块re,您需要的代码如下所示:

import re 
 
m = re.match("^testing", "ptesting_kip-win332") 
if m != None: 
     print ("Uninstalling" + product.Name + "...") 
     result = product.Uninstall() 

你也可以考虑使用字符串函数startswith() 这将像这样工作:

name = "testing_kip-win332" 
 
if name.startswith("testing"): 
    print ("Uninstalling" + product.Name + "...") 
    result = product.Uninstall()