> -----Original Message----- > From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl- > win32-users-boun...@listserv.activestate.com] On Behalf Of p sena > Sent: 02 March 2011 17:16 > To: perl-win32-users@listserv.ActiveState.com > Subject: regex like option *values* > > Hi, > > I want to use option and values like:- > > --option_name abc0[1-9].ctr.{pad,spd}.set.in > > or --option_name abc[01-22].ctr.{pad,spd}.set.in > > or --option_name abcL{1,2,3}.ctr.{pad,spd}.set.in > > or --option_name abcL[1,2,3].ctr.{pad,spd}.set.in > > or --option_name abcL{1,2,3}.ctr.{70,001}.set.in > > etc possibilities. This should in fact expand those option values into > the right number of values/quantities i,e; --option_name will hold > multiple values. Instead of supplying values one after another I just > want to club them in a regex like style. I am already using Getopt::Long. > > What could be best way to handle this type of passing option values? > Is there any existing module for this ?
I could be wrong, but I doubt that an existing module would do what you want. Generating all possible strings that match a regex is hard in the general case, if not impossible. However, if you limit the expressions you want to expand and simplify your syntax a bit, it's not too difficult. Here's a quick hack that, I think, does pretty much what you want. --------------------------------------------------- use strict; use warnings; while (<DATA>) { chomp; print "Expanding: $_\n"; my @result = expand_string($_); print " $_\n" for @result; } # Expand string to array of strings based on lists & ranges in square # brackets. Note recursion not strictly necessary, but it simplifies # the code. sub expand_string { my $str = shift; my @result; if ($str =~ /^(.*?)\[([^]]+)\](.*)$/) { my ($pre, $post) = ($1, $3); my @bits = expand_list($2); foreach my $bit (@bits) { push @result, expand_string("$pre$bit$post"); } } else { push @result, $str; } return @result; } # Return array from comma separated list of strings and ranges. sub expand_list { my @vals = split /\s*,\s*/, $_[0]; my @result; foreach my $v (@vals) { if ($v =~ /^([^-]+)-([^-]+)$/) { push @result, eval "'$1'..'$2'"; die $@ if $@; } else { push @result, $v; } } return @result; } __DATA__ abc0[1-9].ctr.[pad,spd].set.in abc[01-22].ctr.[pad,spd].set.in abcL[1,2,3].ctr.[pad,spd].set.in abcL[1,2,3].ctr.[pad,spd].set.in abcL[1,2,3].ctr.[70,001].set.in --------------------------------------------------- It should work for lists of ranges, and ranges of strings as well as numbers. Regarding incorporating into Getopt::Long, see the Tips and Tricks section of the doco. HTH -- Brian Raven Please consider the environment before printing this e-mail. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs