Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regular Expression Builder

by fruiture (Curate)
on Aug 30, 2002 at 16:50 UTC ( #194172=note: print w/ replies, xml ) Need Help??


in reply to Regular Expression Builder

Well, 'rich36' could be translated to '\w{4}\d{2}' or to '\w{6}' or '.{6}' ... You need to specify that [a-zA-Z] must become \w and [0-9] must become \d ...

A try:

#!/usr/bin/perl use strict; use warnings; { my @classes = ( ['[a-zA-Z]' => '\w'], ['[0-9]' => '\d'], ['\w' => '_'], #that's why order matters ['.' => '.'], ); sub make_regex { local $_ = @_ ? shift : $_; my $result = ''; my $i = -1; while( ++$i < @classes ){ my $p = pos($_) || 0; my ($re,$su) = @{ $classes[$i] }; if( /\G($re+)/g ){ $result .= $su . '{' . length($1) . '}'; $i = -1; } else { pos($_) = $p; } } $result } } printf "%s => %s\n",$_,make_regex for ( 'abc12','123','#+#+#', )

update: corrected &#91; and &#93; again (twice)...

--
http://fruiture.de


Comment on Re: Regular Expression Builder
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-07-05 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls