Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Regular Expression Test

by LanX (Saint)
on May 06, 2017 at 19:46 UTC ( [id://1189686]=note: print w/replies, xml ) Need Help??


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

Hi Discipulus

tybalt89 is combining the \G meta with //gc modifiers

  • \G achors the search at the pos where the last regex left off
  • unless you used the //c modifier, the pos of a previous regex was erased when it didn't match
This allows to split a big complicated regex into multiple perl statements.

And after combining with recursive calls this permits to parse nested structures.

See perldocs for examples and details of this technique (e.g. perlretut ) or Friedl's Regex Book.

Or even better older threads in the monastery :)

You shouldn't be despaired, the demonstrated code is neither easy to read nor maintain.

It should better use //x modifiers and more comments, but obfuscated wizardry is of course cooler! ;-))

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^4: Regular Expression Test
by tybalt89 (Monsignor) on May 07, 2017 at 19:04 UTC

    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])
Re^4: Regular Expression Test
by shmem (Chancellor) on May 06, 2017 at 20:30 UTC
    You shouldn't be despaired, the demonstrated code is neither easy to read nor maintain.

    Depends on who's reading it, and who's maintaining it. If so.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you post live.

      originally from ...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found