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.

In reply to Matching dialplan rules... by athanasia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.