正则表达式需求:匹配不含19/20开头四位年份的括号内容
解决方案:移除文件名中非年份类括号内容
直接用下面这个正则表达式匹配需要删除的冗余内容,将匹配结果替换为空字符串即可:
\s*\((?!(19|20)\d{2}\))[^)]*\)\s*
正则规则拆解(给完全不懂正则的你):
\s*:匹配括号前后的任意空格(不管有几个空格,甚至没有空格都能覆盖)\(:精准匹配左括号((?!(19|20)\d{2}\)):核心判断——排除年份格式,意思是只要括号里是19XX或20XX这种四位数字,就不会被匹配[^)]*:匹配括号里除了右括号)之外的所有内容(就是feat、编号这类冗余信息)\):精准匹配右括号)
实际效果示例:
原文件名:
Abel the Kid - a Piece of My Love (feat. Snoop Dogg) Alex Gaudino feat. Maxine Ashley - I'm in Love (I-039-M) 56K feat. Bejay - Save a Prayer (2003) Mariah Carey feat. Trey Lorenz - I'll Be There (2012) Savage Garden - To the Moon and Back (1997)
处理后文件名:
Abel the Kid - a Piece of My Love Alex Gaudino feat. Maxine Ashley - I'm in Love 56K feat. Bejay - Save a Prayer (2003) Mariah Carey feat. Trey Lorenz - I'll Be There (2012) Savage Garden - To the Moon and Back (1997)
内容的提问来源于stack exchange,提问作者trejder
相关产品推荐
相关产品推荐

