简体   繁体   English

正则表达式不匹配括号中的任何内容,只有字母,数字和空格,并且点后面没有任何内容

[英]Regex to not match anything in brackets, only letters, numbers and spaces, and nothing after a dot

For example, I have a filename [foo] FooBar[!].bar 例如,我有一个文件名[foo] FooBar[!].bar

How can I use regex to match FooBar but not [foo] , [!] or .bar 如何使用正则表达式匹配FooBar而不是[foo][!].bar

BTW, I'm using Python Regex, if that has any bearing. 顺便说一句,我正在使用Python Regex,如果它有任何影响。

Basically, I want to match anything that is not after a dot, or is not in brackets. 基本上,我想匹配任何不在点之后或不在括号中的东西。

fileName = '[abc][def]Real Name[!].exe'
name = re.search('(\[[^]]*\])*([\w\s]+)', fileName).group(2)
print name

This should print 'Real Name' 这应该打印'真实姓名'

(?:^|])(?!\.)([\w\s]+)(?:\[|$)'

这应该匹配由][或者开始和结束字符串并且前面没有点的任何单词+空格组合。

You could replace it all: 你可以全部替换它:

>>> re.sub(r'\[.*?\]|\..*', '', '[foo] FooBar[!].bar')
    ' FooBar'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM