acid06 has asked for the wisdom of the Perl Monks concerning the following question:
my $str = "((A,B,(C,D,(E,F),G),H),(I,J,K))"; our @nodes; our $re; $re = qr{ \( (?: (?: ((?> [^()]+ )) # Non-parens without backtracking (?{ /./ }) # <--- any regex here segfaults )+ | (??{ $re }) # Group with matching parens )* \) }x;
#!/usr/bin/perl use warnings; use strict; use lib '.'; use Bio::Tree::Node; my $str = " \n (\n(A:0.333,B,(C,D,(E, \nF),G),H):0.456,(I,J,K):0.1 +23) \n "; my $current = Bio::Tree::Node->new; my $last; our $re = qr{ \( (?{ $current = $current->add_new_child }) (?: (?: ((?> [^()]+ )) # Non-parens without backtracking (?{ process_children($current, $^N) }) )+ | (??{ $re }) # Group with matching parens )* \)(?{ $current = $current->parent }) }x; $str =~ $re; sub process_children { my ($node_obj, $str) = @_; my @nodes = split '\s*,\s*', $str; for my $node (@nodes) { my ($tag, $contents) = split ':', $node; if ($tag) { $node_obj->add_new_child($tag, $contents) } else { $node_obj->contents($contents); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl segfaulting when using a hairy regex
by Paladin (Vicar) on Mar 09, 2006 at 23:02 UTC | |
|
Re: Perl segfaulting when using a hairy regex
by GrandFather (Saint) on Mar 09, 2006 at 22:59 UTC | |
|
Re: Perl segfaulting when using a hairy regex
by QM (Parson) on Mar 10, 2006 at 17:52 UTC |