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? | [reply] |
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 | [reply] [d/l] |
|
The string i want to convert is
$string = 'gat_i2345'
it has no delimiter
| [reply] |
|
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;.
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
|
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.
| [reply] |
|
my @array = $string =~ /./g;
or
my @array = map /./g, $string;
or
my @array = split(//, $string);
| [reply] [d/l] [select] |
|
You have not clearly described how you want the program to infer what regular expression to generate from the input.
| [reply] |
|
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
| [reply] [d/l] |
|
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). | [reply] [d/l] |
|
# 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/;
| [reply] [d/l] [select] |
|
I thought qr// was a compile time thing. I stand corrected. Thank you.
| [reply] |