re.search()
用于在字符串中查找和返回文件扩展名的正确正则表达式语句是什么.
如:
(.+).(avi|rar|zip|txt)
我需要它来搜索字符串,如果它包含任何avi,rar等)只返回该扩展名.
谢谢!
编辑:应该添加,需要不区分大小写
你需要:
(.)\.(avi|rar|zip|txt)$
Run Code Online (Sandbox Code Playgroud)
注意反斜杠以逃避点.这将使它找到一个文字点而不是任何字符.
要使其不区分大小写,请在搜索调用中使用RE.I标志.
re.search(r'(.)\.(avi|rar|zip|txt)$', string, re.I)
Run Code Online (Sandbox Code Playgroud)