我需要一个正则表达式来选择两个外括号之间的所有文本。
例子: START_TEXT(这里的文本(可能的文本)文本(可能的文本(更多的文本))END_TXT ^ ^
结果: (此处文本(可能的文本)文本(可能的文本(更多的文本)))
我需要一个正则表达式来选择两个外括号之间的所有文本。
例子: START_TEXT(这里的文本(可能的文本)文本(可能的文本(更多的文本))END_TXT ^ ^
结果: (此处文本(可能的文本)文本(可能的文本(更多的文本)))
当前回答
除了bobble bubble的答案之外,还有其他类型的正则表达式支持递归结构。
Lua
使用%b() (%b{} / %b[]作为大括号/方括号):
对于字符串中的s。gmatch(“提取(a (b) c)和f (g)) ((d)”,“% b()”)做打印(s)结束(见演示)
Raku(前Perl6):
不重叠的多个平衡括号匹配:
my regex paren_any { '(' ~ ')' [ <-[()]>+ || <&paren_any> ]* }
say "Extract (a(b)c) and ((d)f(g))" ~~ m:g/<&paren_any>/;
# => (「(a(b)c)」 「((d)f(g))」)
重叠多个平衡括号匹配:
say "Extract (a(b)c) and ((d)f(g))" ~~ m:ov:g/<&paren_any>/;
# => (「(a(b)c)」 「(b)」 「((d)f(g))」 「(d)」 「(g)」)
看到演示。
Python的非正则表达式解决方案
参见poke对如何在平衡括号之间获取表达式的回答。
Java可定制的非正则表达式解决方案
下面是一个可定制的解决方案,允许在Java中使用单个字符文字分隔符:
public static List<String> getBalancedSubstrings(String s, Character markStart,
Character markEnd, Boolean includeMarkers)
{
List<String> subTreeList = new ArrayList<String>();
int level = 0;
int lastOpenDelimiter = -1;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == markStart) {
level++;
if (level == 1) {
lastOpenDelimiter = (includeMarkers ? i : i + 1);
}
}
else if (c == markEnd) {
if (level == 1) {
subTreeList.add(s.substring(lastOpenDelimiter, (includeMarkers ? i + 1 : i)));
}
if (level > 0) level--;
}
}
return subTreeList;
}
}
示例用法:
String s = "some text(text here(possible text)text(possible text(more text)))end text";
List<String> balanced = getBalancedSubstrings(s, '(', ')', true);
System.out.println("Balanced substrings:\n" + balanced);
// => [(text here(possible text)text(possible text(more text)))]
其他回答
这个也有用
re.findall(r'\(.+\)', s)
答案取决于您是需要匹配括号的匹配集,还是只需要匹配输入文本中的第一个开始到最后一个结束的括号。
如果您需要匹配匹配的嵌套括号,那么您需要的不仅仅是正则表达式。-见@dehmann
如果只是先开后关,再见@扎克
决定你想要发生什么:
abc ( 123 ( foobar ) def ) xyz ) ghij
您需要决定在这种情况下您的代码需要匹配什么。
因为js regex不支持递归匹配,我不能使平衡括号匹配工作。
这是一个简单的javascript循环版本,将“method(arg)”字符串转换为数组
push(number) map(test(a(a()))) bass(wow, abc)
$$(groups) filter({ type: 'ORGANIZATION', isDisabled: { $ne: true } }) pickBy(_id, type) map(test()) as(groups)
const parser = str => {
let ops = []
let method, arg
let isMethod = true
let open = []
for (const char of str) {
// skip whitespace
if (char === ' ') continue
// append method or arg string
if (char !== '(' && char !== ')') {
if (isMethod) {
(method ? (method += char) : (method = char))
} else {
(arg ? (arg += char) : (arg = char))
}
}
if (char === '(') {
// nested parenthesis should be a part of arg
if (!isMethod) arg += char
isMethod = false
open.push(char)
} else if (char === ')') {
open.pop()
// check end of arg
if (open.length < 1) {
isMethod = true
ops.push({ method, arg })
method = arg = undefined
} else {
arg += char
}
}
}
return ops
}
// const test = parser(`$$(groups) filter({ type: 'ORGANIZATION', isDisabled: { $ne: true } }) pickBy(_id, type) map(test()) as(groups)`)
const test = parser(`push(number) map(test(a(a()))) bass(wow, abc)`)
console.log(test)
结果就像
[ { method: 'push', arg: 'number' },
{ method: 'map', arg: 'test(a(a()))' },
{ method: 'bass', arg: 'wow,abc' } ]
[ { method: '$$', arg: 'groups' },
{ method: 'filter',
arg: '{type:\'ORGANIZATION\',isDisabled:{$ne:true}}' },
{ method: 'pickBy', arg: '_id,type' },
{ method: 'map', arg: 'test()' },
{ method: 'as', arg: 'groups' } ]
[^\(]*(\(.*\))[^\)]*
[^\(]*匹配字符串开头不是右括号的所有内容,(\(.*\))捕获括在括号中的所需子字符串,[^\)]*匹配字符串末尾不是右括号的所有内容。注意,这个表达式不会试图匹配括号;一个简单的解析器(参见dehmann的回答)将更适合于此。
我想添加这个答案,以便快速参考。请随时更新。
.NET Regex使用平衡组:
\((?>\((?<c>)|[^()]+|\)(?<-c>))*(?(c)(?!))\)
其中c用作深度计数器。
在Regexstorm.com上进行演示
堆栈溢出:使用正则表达式来平衡匹配括号 Wes令人困惑的博客:平衡结构与。net正则表达式的匹配 Greg Reinacker的Weblog:正则表达式中的嵌套结构
使用递归模式的PCRE:
\((?:[^)(]+|(?R))*+\)
演示在regex101;或无交替的:
\((?:[^)(]*(?R)?)*+\)
演示在regex101;或为表演而展开:
\([^)(]*+(?:(?R)[^)(]*)*+\)
演示在regex101;模式被粘贴在(?R)处,它表示(?0)。
Perl, PHP, notepad++, R: Perl =TRUE, Python: PyPI正则表达式模块与(?V1)的Perl行为。 (新版本的PyPI regex包已经默认为this→DEFAULT_VERSION = VERSION1)
Ruby使用子表达式调用:
与Ruby 2.0 \g<0>可以用来调用完整的模式。
\((?>[^)(]+|\g<0>)*\)
在Rubular演示;Ruby 1.9只支持捕获组递归:
(\((?>[^)(]+|\g<1>)*\))
Rubular的演示(从Ruby 1.9.3开始进行原子分组)
API JavaScript
XRegExp.matchRecursive(str, '\\(', '\\)', 'g');
Java: @jaytea使用前向引用的有趣想法。
不递归最多3层嵌套: (JS, Java和其他类型的正则表达式)
为了防止不平衡时失控,只在最内层[)(]上使用*。
\((?:[^)(]|\((?:[^)(]|\((?:[^)(]|\([^)(]*\))*\))*\))*\)
演示在regex101;或展开以获得更好的性能(首选)。
\([^)(]*(?:\([^)(]*(?:\([^)(]*(?:\([^)(]*\)[^)(]*)*\)[^)(]*)*\)[^)(]*)*\)
演示在regex101;需要根据需要添加更深层次的嵌套。
参考-这个正则表达式是什么意思?
递归正则表达式 Regular- expressions .info -正则表达式递归 精通正则表达式- Jeffrey E.F. Friedl 1 2 3 4