我想要一個正則表達式來匹配分隔值和一些可以包含分隔符的受保護值。正則表達式匹配受保護的分隔值
例如:
"A,B,{C,D,E},F"
會給:
- 「A」
- 「B」
- 「{C,d,E}」
- 「F」
懇求本身注意保護值可以嵌套,如下:
"A,B,{C,D,{E,F}},G"
會給:
- 「A」
- 「B」
- 「{C,d,{E,F }}」
- 「G」
我已經編碼的功能與角色迭代如下:
sub Parse
{
my @item;
my $curly;
my $string;
foreach(split //)
{
$_ eq "{" and ++$curly;
$_ eq "}" and --$curly;
if(!$curly && /[,:]/)
{
push @item, $string;
undef $string;
next;
}
$string .= $_;
}
push @item, $string;
return @item;
}
但是它肯定會對正則表達式更好。
你有'{{A},{B}}'作爲輸入? – nhahtdh 2012-07-27 07:34:22
是的,我可以嵌套'{}',例如'A,B,{D,{E,F},G},H'應該給'A''B'' {D,{E,F} ,G}''H' – Christophe 2012-07-27 07:53:30
正則表達式不能處理任意級別的嵌套。您必須指定最大嵌套級別,或者使用適當的解析器。 – tripleee 2012-07-27 14:23:43