Regexp捕获分隔符之间的字符串

gnz*_*lbg 4 regex

这个问题中,提供了一个用于在分隔符之间捕获字符串的正则表达式:

测试: This is a test string [more or less]

正则表达式: (?<=\[)(.*?)(?=\])

返回: more or less

如果要捕获的字符串还包含分隔符怎么办?

测试1: This is a test string [more [or] less]

返回1: more [or] less

测试2: This is a test string [more [or [and] or] less]

返回2: more [or [and] or] less

和多个括号?

测试3: This is a test string [more [or [and] or] less] and [less [or [and] or] more]

回归3​​ : more [or [and] or] less ,less [or [and] or] more

哪个正则表达式会这样做?或者哪个小ruby/python脚本可以做到这一点?

Mik*_*keM 6

在javascript中

var str = 'This is a test string [more [or [and] or] less]';    
str = str.match( /\[(.+)\]/ )[1];
// "more [or [and] or] less"
Run Code Online (Sandbox Code Playgroud)

如果省略?,.+则会贪婪地匹配到最后一个].

在python中

str = "This is a test string [more [or [and] or] less]"
re.search( "(?<=\[).+(?=\])", str ).group()
// "more [or [and] or] less"
Run Code Online (Sandbox Code Playgroud)

更新多个嵌套括号

在javascript中

var matches = [],
    str = 'This is a test string [more [or [and] or] less] and [less [or [and] or] more] and [more]';

str.replace( /\[([^\]]*\[?[^\]]*\]?[^[]*)\]/g, function ( $0, $1 ) {
    $1 && matches.push( $1 );
});

console.log( matches );
// [ "more [or [and] or] less", "less [or [and] or] more", "more" ]
Run Code Online (Sandbox Code Playgroud)

在python中

import re
str = 'This is a test string [more [or [and] or] less] and [less [or [and] or] more] and [more]'

matches = re.findall( r'\[([^\]]*\[?[^\]]*\]?[^[]*)\]', str )

print matches
# [ 'more [or [and] or] less', 'less [or [and] or] more', 'more' ]
Run Code Online (Sandbox Code Playgroud)