Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Regular Expression Test

by tybalt89 (Monsignor)
on May 07, 2017 at 19:04 UTC ( [id://1189759]=note: print w/replies, xml ) Need Help??


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 (Saint) on May 07, 2017 at 19:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189759]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-19 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found