in reply to Re^2: how can i split a string to an array and convert it into regular expression
in thread how can i split a string to an array and convert it into regular expression

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
  • Comment on Re^3: how can i split a string to an array and convert it into regular expression
  • Download Code

Replies are listed 'Best First'.
Re^4: how can i split a string to an array and convert it into regular expression
by Anonymous Monk on Jan 21, 2008 at 14:39 UTC