好吧,我有一个奇怪的,我已经坚持了一段时间了(我猜周五下午脑子不灵了)。
有谁知道如何解析字符串并删除括号内的所有文本而不删除括号本身......但删除内部发现的括号。
即。
myString = "this is my string (though (I) need (help) fixing it)"
运行完我想要的内容后,它看起来像:
myString = "this is my string ()"
将这两个括号保留在那里非常重要。
最佳答案
模块Regexp::Common处理超过 1 个顶级括号。
use strict;
use warnings;
use Regexp::Common qw/balanced/;
my @strings = (
'111(22(33)44)55',
'a(b(c(d)(e))f)g(h)((i)j)',
'this is my string (though (I) need (help) fixing it)',
);
s/$RE{balanced}{-parens=>'()'}/()/g for @strings;
print "$_\n" for @strings;
输出:
111()55 a()g()() this is my string ()
关于perl - 删除括号内的文本,但不删除 Perl 中的括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2255673/