#!/usr/bin/perl use strict; use warnings; my %Build = ( jj => '0-9', ); # Example 5 does not like this and i need the code to handle more then + 1 # my %Build = ( # jj => '0-9', # kk => 'a-z', # ); my $message = '[445] [5353453] [fff] [445]'; my $message2 = $message; my $message3 = $message; my $message4 = $message; my $message5 = $message; my $message6 = $message; # jump to Example 1, It should make more sense then # Example 6 # This is the code i made to get around these problems # what is strange about this file is if this code is last it did not w +ork # Look for "# Not Here" for the location it does not work at # Also this looks to me to be to big and the size of Example 5 looks b +etter # but i don t like having "use re 'eval';" because it seems to produce + bugs in this file # and if it does it in this file, I don t want to use it. # is there another way? foreach my $b_key2 (keys %Build) { my @re_match = (); @re_match = ($message6 =~ m{\[([$Build{$b_key2}]+)\]} +isg); my %speed_fix = (); if (@re_match) { foreach my $m_check (@re_match) { next if $speed_fix{$m_check}; my $built = ''; $built = return_number($m_check); if ($built) { $message6 =~ s{\[([$m_check]+)\]}{$built}isg; $speed_fix{$m_check} = 1; } } } } # Example 1 # the first test # but needs to be in a foreach and inside (?{ code }) will call a sub $message =~ s{\[([0-9]+)\](?(?{$+ ne ''})|(?!))}{$+}gs; # Example 2 # this calls a sub but removes what it matched my $vid = ''; $message2 =~ s{\[([0-9]+)\](?(?{ $vid = return_number($+); $vid ne '' +})|(?!))}{$vid}gs; # if a space was added it returns the match $vid = ''; # Example 3 $message3 =~ s{\[([0-9]+)\](?(?{ $vid = return_number($+); $vid ne '' +})|(?!))}{$vid }gs; # space here # Example 4 # the start of what i wanted to do but has an error so its commented o +ut # # Error: Eval-group not allowed at runtime, use re 'eval' # foreach my $b_key (keys %Build) { # my $vid = ''; # $message4 =~ s{\[([$Build{$b_key}]+)\](?(?{$vid = $1; $v +id ne ''})|(?!))}{$vid}gs; # } # Example 5 # With use re 'eval'; # almost works but has a few bugs and causes a bug in Example 6. # if Example 6 was under comment "# Not Here" then Example 6 will not +work # Next Jump to Example 6 at the top foreach my $b_key (keys %Build) { my $re_pat = '(?(?{my $vid = return_number($+); $vid ne \' +\'})|(?!))'; use re 'eval'; $message5 =~ s{\[([$Build{$b_key}]+)\]${re_pat}}{$vid}gs; no re 'eval'; } # Not Here print "Content-type: text/html\n\n"; print "<html><h1>Example 1: $message,<br> Example 2: $message2,<br> Ex +ample 3: $message3,<br> Example 4: $message4,<br> Example 5: $message +5,<br> Example 6: $message6</h1></html>\n"; sub return_number { my $numbers = shift; return $numbers; }
In reply to Regex (?{ code }) and use re 'eval' by SFLEX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |