Regex将在一对方括号中获取文本

时间:2022-07-20 21:43:13

In the text below how do I get the text inside the first pair of square brackets

在下面的文本中,如何在第一对方括号中获取文本

xxxx [I can be any text and even have digits like 0 25 ] [sdfsfsf] [ssf sf565wf]

xxxx[我可以是任何文本,甚至是0 25][sdfsfssf] [ssf sf565wf]

This is what I tried. But it goes till the last square bracket.

这就是我所尝试的。但是直到最后一个方括号。

.*\[.*]

What i want selected is

我想要的是!

[I can be any text and even have digits like 0 25 ]

3 个解决方案

#1


2  

If you don't want to go past the closing square bracket, use [^\]]* in place of .*:

如果你不想去过去结束方括号,使用[^ \]]*代替。*:

^[^\[]*(\[[^\]]*])

Add ^ anchor at the beginning if you would like to search multiple lines.

添加^锚一开始如果你想搜索多个行。

Add a capturing group around the square brackets, and get the content of that group to obtain the text that you need.

在方括号周围添加捕获组,并获取该组的内容以获得所需的文本。

Demo.

演示。

#2


1  

Another one with DEMO. A bit complicated though:

另一个演示。不过有点复杂:

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

EXPLANATION

解释

(\[[^\]]+\])            #capturing group
                        #match first [] pair

[^\[\]]*                #match characters except ] and [

(?:\[[^\]].*\])         #non-capturing group
                        #match all the rest [] pairs
                        #this is a greedy match

#3


0  

* and + are 'greedy' by default, so they try to match as much text as possible. If you want them to match as little as possible, you can make them non-greedy with ?, eg .*\[.*?\]. Also, the .* at the beginning matches any number of any characters before the opening square bracket, so this regex will match all text up to a ']' as long as there is a '[' somewhere before the ']'. If you only want to match the brackets and their contents, you want simply \[.*?\].

*和+默认是“贪婪”的,所以他们尽量匹配尽可能多的文本。如果你想让它们尽可能少地匹配,你可以用? *\[. \]来让它们不贪心。同样,在开头的.*匹配左方括号前的任何字符数,因此,只要在' ' '之前有' ' ' ',这个regex就会将所有文本匹配为']' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '。如果您只想匹配括号及其内容,只需[.*?\]。

Non-greedy modifiers with ? are not supported in all regex engines; if it's available to you you should write it with ? because it makes your intent clearer, but if you are using a simpler regex engine you can achieve the same effect by using \[[^\]]*\] instead. This is a negated character class, which matches as many as possible of any character except ']'.

贪婪的修饰符?不支持所有regex引擎;如果有的话,你应该用它来写吗?因为它使你的意图更清晰,但如果您使用的是一个简单的正则表达式引擎可以达到相同的效果通过使用\[[^ \]]* \]。这是一个被否定的字符类,它尽可能多地匹配除']'之外的任何字符。

#1


2  

If you don't want to go past the closing square bracket, use [^\]]* in place of .*:

如果你不想去过去结束方括号,使用[^ \]]*代替。*:

^[^\[]*(\[[^\]]*])

Add ^ anchor at the beginning if you would like to search multiple lines.

添加^锚一开始如果你想搜索多个行。

Add a capturing group around the square brackets, and get the content of that group to obtain the text that you need.

在方括号周围添加捕获组,并获取该组的内容以获得所需的文本。

Demo.

演示。

#2


1  

Another one with DEMO. A bit complicated though:

另一个演示。不过有点复杂:

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

EXPLANATION

解释

(\[[^\]]+\])            #capturing group
                        #match first [] pair

[^\[\]]*                #match characters except ] and [

(?:\[[^\]].*\])         #non-capturing group
                        #match all the rest [] pairs
                        #this is a greedy match

#3


0  

* and + are 'greedy' by default, so they try to match as much text as possible. If you want them to match as little as possible, you can make them non-greedy with ?, eg .*\[.*?\]. Also, the .* at the beginning matches any number of any characters before the opening square bracket, so this regex will match all text up to a ']' as long as there is a '[' somewhere before the ']'. If you only want to match the brackets and their contents, you want simply \[.*?\].

*和+默认是“贪婪”的,所以他们尽量匹配尽可能多的文本。如果你想让它们尽可能少地匹配,你可以用? *\[. \]来让它们不贪心。同样,在开头的.*匹配左方括号前的任何字符数,因此,只要在' ' '之前有' ' ' ',这个regex就会将所有文本匹配为']' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '。如果您只想匹配括号及其内容,只需[.*?\]。

Non-greedy modifiers with ? are not supported in all regex engines; if it's available to you you should write it with ? because it makes your intent clearer, but if you are using a simpler regex engine you can achieve the same effect by using \[[^\]]*\] instead. This is a negated character class, which matches as many as possible of any character except ']'.

贪婪的修饰符?不支持所有regex引擎;如果有的话,你应该用它来写吗?因为它使你的意图更清晰,但如果您使用的是一个简单的正则表达式引擎可以达到相同的效果通过使用\[[^ \]]* \]。这是一个被否定的字符类,它尽可能多地匹配除']'之外的任何字符。