Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by Anonymous Monk
on Dec 08, 2007 at 17:41 UTC ( [id://655847]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,
I am newbee in perl i am actually using <STDIN> to take the input
when the person enter data to it it is taken as a string
i want to convert the string to an array
the $string appears like 'gat_12345'
i want to split it into an array becoz i want to convert the
user input into regular expression with some loops
is there any other way i could convert the input to a reguler expression. Thanks
  • Comment on how can i split a string to an array and convert it into regular expression

Replies are listed 'Best First'.
Re: how can i split a string to an array and convert it into regular expression
by runrig (Abbot) on Dec 08, 2007 at 18:53 UTC
    Perhaps you want to take a look at the split function (to split the data) and the qr operator (to create regexes). If that doesn't help, perhaps you write a short example of how you are trying to split the data, what you want to split it into, and what sort of regular expressions you want, and how you want to use them?
Re: how can i split a string to an array and convert it into regular expression
by vitaly (Acolyte) on Dec 08, 2007 at 18:50 UTC
    I do not understand exactly what string you want to convert, but in order to convert a string into array you can use
    @array = split /delimeter/, $my_string;
    So you need to figure out what delimeter is. By default it is space.
    Vitaly
      The string i want to convert is $string = 'gat_i2345'
      it has no delimiter
        What do you want the array to contain? ('gat', 'i2345')? Then _ is the delimiter. If you want to add the entire string to the array, simply use push @array, $string;.
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
    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

Re: how can i split a string to an array and convert it into regular expression
by CountZero (Bishop) on Dec 08, 2007 at 19:23 UTC
    If you could give us the contents of the array you wish 'gat_12345' to be split into, we can perhaps give you some useful answers. Now we are just second guessing you.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      the array should contain (g,a,t,_,1,2,3,4,5)
      and the regex should be like /\b\w+_\d+\b/i
      Thanks for your help.

        To seperate the string into individual characters, you can use

        my @array = $string =~ /./g;
        or
        my @array = map /./g, $string;
        or
        my @array = split(//, $string);
        You have not clearly described how you want the program to infer what regular expression to generate from the input.
        Your regex groups characters using the + operator. Splitting into a list isn't the first way I would try this. Instead, I might try:
        my @groups = qw'[a-zA-Z]+ \d+ \s+'; my $matcher_pattern = join('|', (map {"($_)"} @groups, '.')); my $pattern = ''; MATCH: while ($string =~ /$matcher_pattern/g) { no strict 'refs'; for my $index (1.. scalar @groups) { if ($$index) { $pattern .= $groups[$index - 1]; next MATCH; } } $pattern .= quotemeta ${scalar @groups + 1}; } $regex = qr/$pattern/
        Update:Fixed off by one error. Update 2:Fixed metachar issue
Re: how can i split a string to an array and convert it into regular expression
by plobsing (Friar) on Dec 08, 2007 at 19:19 UTC
    If I understand correctly, every line on STDIN is a possible match in a regex you are building. Then one solution is this:
    my @arry = <>; my $pattern = join('|', @arry); my $regex = eval("qr/$pattern/");
    That of course assumes that your input isn't doing anything evil (like having a '/' in it).

      You forgot to strip the line endings and you needlessly invoke the Perl parser and compiler in addition to the regex parser and compiler when only the latter is needed.

      # If we are reading in regexs. chomp( my @arry = <> ); my $pattern = join('|', @arry); my $regex = qr/$pattern/;
      # If we are reading in text strings. chomp( my @arry = <> ); my $pattern = join('|', map quotemeta, @arry); my $regex = qr/$pattern/;
        I thought qr// was a compile time thing. I stand corrected.
        Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found