正则表达式提取方括号之间的文本

时间:2021-07-09 21:43:46

Simple regex question. I have a string on the following format:

简单的正则表达式问题。我有以下格式的字符串:

this is a [sample] string with [some] special words. [another one]

What is the regular expression to extract the words within the square brackets, ie.

在方括号内提取单词的正则表达式是什么?

sample
some
another one

Note: In my use case, brackets cannot be nested.

注意:在我的用例中,括号不能嵌套。

7 个解决方案

#1


507  

You can use the following regex globally:

您可以在全球使用以下regex:

\[(.*?)\]

Explanation:

解释:

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • \[:]是元字符,如果你想匹配它,需要转义。
  • (.*?) : match everything in a non-greedy way and capture it.
  • (. .*?):以非贪婪的方式匹配所有事物并捕获它。
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.
  • 是一个元字符,如果你想匹配它的话需要转义。

#2


71  

This should work out ok:

这应该没问题:

\[([^]]+)\]

#3


47  

(?<=\[).+?(?=\])

will capture content without brackets

是否会捕获没有括号的内容

(?<=[) - positive lookbehind for [

(?<=[) -对[

.*? - non greedy match for the content

. * ?-内容的非贪婪匹配

(?=]) - positive lookahead for ]

(=) -积极的展望]

EDIT: for nested brackets the below regex should work:

编辑:对于嵌套的括号,下面的regex应该可以工作:

(\[(?:\[??[^\[]*?\]))

#4


29  

Can brackets be nested?

可以嵌套的括号吗?

If not: \[([^]]+)\] matches one item, including square brackets. Backreference \1 will contain the item to be match. If your regex flavor supports lookaround, use

如果不是:\[([^]]+)\]匹配一个项目,包括方括号。Backreference \1将包含要匹配的项目。如果您的regex风味支持查找,请使用

(?<=\[)[^]]+(?=\])

This will only match the item inside brackets.

这将只匹配括号内的项目。

#5


8  

(?<=\().*?(?=\)) works good as per explanation given above. Here's a Python example:

(?<=\().*?(?=\))这是一个Python的例子:

import re 
str =    "Pagination.go('formPagination_bottom',2,'Page',true,'1',null,'2013')"
re.search('(?<=\().*?(?=\))', str).group()
"'formPagination_bottom',2,'Page',true,'1',null,'2013'"

#6


3  

This code will extract the content between square brackets and parentheses

这段代码将提取方括号和圆括号之间的内容

(?:(?<=\().+?(?=\))|(?<=\[).+?(?=\]))

(?: non capturing group
(?<=\().+?(?=\)) positive lookbehind and lookahead to extract the text between parentheses
| or
(?<=\[).+?(?=\]) positive lookbehind and lookahead to extract the text between square brackets

#7


1  

([[][a-z \s]+[]])

Above should work given the following explaination

鉴于下面的解释,上面的工作应该可以完成

  • characters within square brackets[] defines characte class which means pattern should match atleast one charcater mentioned within square brackets

    方括号[]中的字符定义了字符类,这意味着模式应该匹配方括号中提到的至少一个字符

  • \s specifies a space

    \ s指定了一个空间

  •  + means atleast one of the character mentioned previously to +.

    +表示前面提到的字符中至少有一个是+。

#1


507  

You can use the following regex globally:

您可以在全球使用以下regex:

\[(.*?)\]

Explanation:

解释:

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • \[:]是元字符,如果你想匹配它,需要转义。
  • (.*?) : match everything in a non-greedy way and capture it.
  • (. .*?):以非贪婪的方式匹配所有事物并捕获它。
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.
  • 是一个元字符,如果你想匹配它的话需要转义。

#2


71  

This should work out ok:

这应该没问题:

\[([^]]+)\]

#3


47  

(?<=\[).+?(?=\])

will capture content without brackets

是否会捕获没有括号的内容

(?<=[) - positive lookbehind for [

(?<=[) -对[

.*? - non greedy match for the content

. * ?-内容的非贪婪匹配

(?=]) - positive lookahead for ]

(=) -积极的展望]

EDIT: for nested brackets the below regex should work:

编辑:对于嵌套的括号,下面的regex应该可以工作:

(\[(?:\[??[^\[]*?\]))

#4


29  

Can brackets be nested?

可以嵌套的括号吗?

If not: \[([^]]+)\] matches one item, including square brackets. Backreference \1 will contain the item to be match. If your regex flavor supports lookaround, use

如果不是:\[([^]]+)\]匹配一个项目,包括方括号。Backreference \1将包含要匹配的项目。如果您的regex风味支持查找,请使用

(?<=\[)[^]]+(?=\])

This will only match the item inside brackets.

这将只匹配括号内的项目。

#5


8  

(?<=\().*?(?=\)) works good as per explanation given above. Here's a Python example:

(?<=\().*?(?=\))这是一个Python的例子:

import re 
str =    "Pagination.go('formPagination_bottom',2,'Page',true,'1',null,'2013')"
re.search('(?<=\().*?(?=\))', str).group()
"'formPagination_bottom',2,'Page',true,'1',null,'2013'"

#6


3  

This code will extract the content between square brackets and parentheses

这段代码将提取方括号和圆括号之间的内容

(?:(?<=\().+?(?=\))|(?<=\[).+?(?=\]))

(?: non capturing group
(?<=\().+?(?=\)) positive lookbehind and lookahead to extract the text between parentheses
| or
(?<=\[).+?(?=\]) positive lookbehind and lookahead to extract the text between square brackets

#7


1  

([[][a-z \s]+[]])

Above should work given the following explaination

鉴于下面的解释,上面的工作应该可以完成

  • characters within square brackets[] defines characte class which means pattern should match atleast one charcater mentioned within square brackets

    方括号[]中的字符定义了字符类,这意味着模式应该匹配方括号中提到的至少一个字符

  • \s specifies a space

    \ s指定了一个空间

  •  + means atleast one of the character mentioned previously to +.

    +表示前面提到的字符中至少有一个是+。