Such a regular expression is quite simple to make (see perlre for a long explanation). First we start with the RE for one single allowed character. To match a set or a range of characters, we must use the [ and ] chars :

[A-Z0-9 ,"&-']
Now we want this RE to match at least four and at maximum 16 characters :
/^[A-Z0-9 ,"&-']{4,16}$/
The braces tell Perl to match the previous part of the RE (our set of valid characters) at least 4 times and at maximum 16 times.

There are of course other ways, for example the more clumsy way of doing it like this :

# Please excuse the long line ;) /^[A-Z0-9 ,"&-'][A-Z0-9 ,"&-'][A-Z0-9 ,"&-'][A-Z0-9 ,"&-'][A-Z0-9 ," +&-']?[A-Z0-9 ,"&-']? ... repeat total 12 times ... [A-Z0-9 ,"&-']?$/
Here, the ? tells Perl to optionally match the previous set, in fact, the ? is equivalent to {0,1}.

If you find yourself struggling with REs often, you might want to take a look at Mastering Regular Expressions by Jeffrey Friedel (It is published by O'Reilly, and has a big owl on the cover) - this book covers everything about REs, but I don't know how good it is for beginners.

Update: Here is a direct link to the book.

Update: perlcgi told me that lowercase characters should be allowed as well. D'oh ;) Here we go again :

# The set for the characters gets changed : [A-Za-z0-9 ,"&-'] # and the RE then looks like this : /^[A-Za-z0-9 ,"&-']{4,16}$/ # and we can modify the RE, so that A-Z also matches # a-z with the "i"-modifyer : /^[A-Z0-9 ,"&-']{4,16}$/i # or equivalent /^[a-z0-9 ,"&-']{4,16}$/i


In reply to Re: Making rules in Regex for string content and size by Corion
in thread Makeing rules in Regex for string content and size by Speedfreak

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.