正则表达式捕获分隔符之间的字符串

时间:2013-02-22 18:55:44

标签: regex

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

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

Regexp: (?<=\[)(.*?)(?=\])

退货: 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] lessless [or [and] or] more

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

1 个答案:

答案 0 :(得分:6)

在javascript中

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

如果省略?.+将贪婪地匹配到最后]

在python中

str = "This is a test string [more [or [and] or] less]"
re.search( "(?<=\[).+(?=\])", str ).group()
// "more [or [and] or] less"
多个嵌套括号的

更新

在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" ]

在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' ]