For example, I have a filename [foo] FooBar[!].bar
How can I use regex to match FooBar
but not [foo]
, [!]
or .bar
BTW, I'm using Python Regex, if that has any bearing.
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.