http://qs1969.pair.com?node_id=1189759


in reply to Re^3: Regular Expression Test
in thread Regular Expression Test

Could be worse :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1189605 use strict; use warnings; $| = 1; sub err { die "ERROR: ", s/\G/<@_>/r, "\n" } sub crossmap # this is clear, isn't it? { my ($left, $right) = @_; [ map { my $prefix = $_ ; map $prefix.$_, @$right } @$left ]; } sub expr { my $answer = [ '' ]; while( /\G ( \| (?{ 'ALTERNATION' }) | \d+ (?{ 'STRING' }) | \[ \d+ \] (?{ 'CHARACTERCLASS' }) | \( (?{ 'PARENTHESIS' }) ) /gcx ) { goto $^R; ALTERNATION: $answer = [ @$answer, @{ expr() } ]; next; STRING: $answer = crossmap $answer, [ $1 ]; next; CHARACTERCLASS: $answer = crossmap $answer, [ $1 =~ /\d/g ]; next; PARENTHESIS: $answer = crossmap $answer, expr(); /\G\)/gc or err "missing ')'"; } return $answer; } while(<DATA>) { chomp; print "$_ => "; my $answer = expr(); /\G\z/gc or err "incomplete parse"; local $" = ','; print "@$answer\n"; } __DATA__ (65|70) (3[678]|4[1678]) 5[45] (6[4569]|7[01])

Replies are listed 'Best First'.
Re^5: Regular Expression Test
by LanX (Sage) on May 07, 2017 at 19:26 UTC