Dear fellow monks,
I need to write a bit of code that checks if a string matches a set of dialplan rules, the following:
o The string should not be empty
o The string could start with a single underscore (_)
o It may contain digits, # or *
o Its numeric part (there could be another part, the one described by the rule below) may end with a range of digits in the format [x-y], where x can be one or more digits and y should be a single digit,
o The string could end with a "character part" containing only the following characters : . (only once), ! (only once) or X (maybe more than one)
** With the term numeric part, which is not so successful, I admit, I refer to the substring that contains #,*,digits or the [x-y] pattern. Hashes (#) and asterisks (*) may mix inside the numeric part but cannot be a part of [x-y].
Some examples of allowed strings:
100
_199
_2XXXXX
800!
_*34#
_*3*#2
_##34[ 12-5].
After checking the validity of the string, I should also fill in two variables, say $res and $maxdigs. The first variable, $res, should contain the "numeric part" i.e. in my previous examples "100", "199", "2", "800", "*34#", "*3*#2", "##34[ 12-5]" and the second variable, $maxdigs should contain "", "", "XXXXX", "!", "", "", "." respectively.
I have written the following bit of code ($pattern is my input string):
my ($res, $maxdigs, $dummy, $silly);
($res, $dummy, $silly, $maxdigs) = $pattern=~/^_*((\d*|#*|\**)+(\[\d+\
+-\d\])?)(X*\.?\!?)$/g;
This bit works in general, however I always fear I have missed something when it comes to perl regular expressions... So, if anyone has the time/appetite to question my code, I would be really really indebted ;-).
Athanasia
Update: Thanks to all who responded so quickly. Imagine I did not even know of the /x operator which obviously could make my life so much simpler! Obviously, my rules were not very strictly explained, thus, I have updated the original query. In any case, I already found some problems in my code thanks to your suggestions.