Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: how can i split a string to an array and convert it into regular expression

by mwah (Hermit)
on Dec 08, 2007 at 20:13 UTC ( [id://655876]=note: print w/replies, xml ) Need Help??


in reply to how can i split a string to an array and convert it into regular expression

i want to split it into an array becoz i want to convert the user input into regular expression with some loops

You don't need to split into an array of characters for performing the task you described. The "program" you'll need looks probably like:

... my $input = <STDIN>; chomp $input; my $regex; $regex = transform_string_to_optimal_matching_regex($input); if( $input =~ /$regex/ ) { print "regex |$regex| to input |$input| mapping succeeded\n"; } ...

Perl is really good at finding strings that match a given regular expression, but rather bad at finding regular expressions that *would match* given strings 'optimally'. The remaining task for you is therefore to enter your favorite algorithm to do that between the curly braces of the subroutine 'transform_string_to_optimal_matching_regex()':

sub transform_string_to_optimal_matching_regex { my $given_string = shift; my $regex = ''; my $optimal_solution_found = 0; while(! $optimal_solution_found ) { # # construct optimal regex by some # stochastic algorithm, possibly # long running Monte Carlo-solver # } return $regex; }

This is really an interesting thing to do. Maybe there are some approaches already published (none of which I know from).

Regards

mwa

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found