I recently started working on a CGI program that acted as a gateway between a few Python clients and a large number of XML files on a server. When I started writing the parameter checking code I found myself writing virtually the same code for each subroutine. Having a single separate sub that was flexible enough to check all parameters would keep the code far simpler, shorter, and more maintainable.

What I quickly threw together was a sub that takes a list of acceptable symbols and two numbers indicating whether or not alpha and numeric characters are allowed in the given parameter. It then generates a very simple regex based on the input.

This seems like it would be a fairly common task (there's even a brief mention of it in Programming Perl 3rd ed.) but a quick search here didn't turn up much other than generating regexes? which wasn't quite what I was looking for. Does anyone routinely generate regexes, and if so what types of tasks have you found generating them most useful for. Also, are there any other examples and/or modules available that I should take a look at? Thanks.

Here's the code (any suggestions are greatly appreciated):

use diagnostics; use strict; use warnings; my @randomParameters = ('Abc.123', '../../', 'a@b.c', '789'); my @allowedSymbols = ('.', '@'); my (@goodParams, @badParams); checkParam($_, 1, 1, @allowedSymbols) for (@randomParameters); print $_, " is good\n" for @goodParams; print $_, " is bad\n" for @badParams; sub checkParam { my $param = shift; my $alpha = shift; # if true allow alpha my $numeric = shift; # if true allow numeric my @symbols = @_; # allow all symbols in @_ my $regex = ""; $regex .= '([a-zA-Z' if $alpha; $regex .= '0-9' if $numeric; for my $symbol (@symbols) { $regex .= $symbol; } $regex .= ']*)'; my $pattern = qr/^${regex}$/; if ($param =~ /$pattern/) { push(@goodParams, $param); } else { push(@badParams, $param); } }

In reply to Generating Regular Expressions by cjf-II

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.