简体   繁体   English

删除方括号及其间的内容

[英]Removing square brackets and what's between them

I'm using the Mac App Name Mangler . 我正在使用Mac App Name Mangler It allows the use of regex. 它允许使用正则表达式。 I have a bunch of files and folders that contain some thing like the following. 我有一堆包含如下内容的文件和文件夹。

File Name [24.9MB].ext 文件名[24.9MB] .ext
Folder Name [1GB] 资料夹名称[1GB]

I want the regex to remove the brackets and what's between them. 我希望正则表达式删除括号以及括号之间的内容。 Also if there's a space before the brackets would be nice if it was deleted as well. 另外,如果括号之前还有一个空格,那么也可以删除它。 Thus becoming... 因此成为...

File Name.ext 文件名
Folder Name 文件夹名称

I've read a bit about regex now and realized that the bracket itself is part of the syntax. 我现在已经阅读了一些有关正则表达式的内容,并且意识到括号本身是语法的一部分。 I need to get around that and what is contained in them. 我需要解决这些问题以及其中包含的内容。 Any help would be appreciated. 任何帮助,将不胜感激。 Having to manually cleanup hundreds of thousands of files and folder names would be too much. 必须手动清理成千上万的文件和文件夹名称,这将太多。

Using the Find and Replace function: 使用查找和替换功能:

Find: \s*\[[^]]*]
Replace with: 

Regular expression: 正则表达式:

\s*            whitespace (\n, \r, \t, \f, and " ") (0 or more times)
 \[            '['
  [^]]*        any character except: ']' (0 or more times)
  ]            ']'

To match the brackets and it's content, the regex will be \\[[^\\]]+\\] . 为了匹配括号及其内容,正则表达式将为\\[[^\\]]+\\] Now if you want to catch the space before the bracket, then you can use [ ]* . 现在,如果要在方括号前留空格,则可以使用[ ]* It means zero or more spaces. 这意味着零个或多个空格。

So, combining the both, your search regex will be: 因此,结合两者,您的搜索正则表达式将为:

[ ]*\[[^\]]+\]

FYI: [^\\]]+ means any character except the ] having one or more times. 仅供参考: [^\\]]+表示除]以外的任何字符有一次或多次。

Use a "non capture group" for the brackets: What is a non-capturing group? 括号中使用“非捕获组”: 什么是非捕获组? What does a question mark followed by a colon (?:) mean? 问号后跟冒号(?:)是什么意思?

http://rubular.com/r/bVhVbKomKm http://rubular.com/r/bVhVbKomKm

/(.+)(?:\s?\[[^\]]+\])(.+)?/

# Match 1
# 1.    File Name
# 2.    .ext
#
# Match 2
# 1.    Folder Name
# 2.     

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

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