[Baypiggies] quick question: regex to stop naughty control characters

Dennis Reinhardt DennisR at dair.com
Wed Apr 25 22:02:55 CEST 2007


At 12:44 PM 4/25/2007, Shannon -jj Behrens wrote:
>using FormEncode with a regex that looks like r".{1,128}$".

First we have to re-write this.  This regex matches on any string having 
between 1 and 128 characters immediately preceding the end of string.  So, 
a string 10 characters long would match because a length 10 string has 1 
character preceding end.  A string 1000 characters long would match as well.

A regex of r"^.{1,128}$" would match only when there are between 1 and 128 
characters and no more or no less between start and end.

Now, we can define exceptions which prevent matching.  A regex of 
r"^[^\r\n]{1,128}$" will match the same size strings but fail if either an 
embedded CR or LF is included.  The extension to other control characters 
is easy.

Yeah, I know I used "^" twice to mean two different things (start of string 
and not, respectively).  That is just regex language, not a mistake on my part.

Regards, Dennis


  ---------------------------------
| Dennis    | DennisR at dair.com    |
| Reinhardt | http://www.dair.com |
  ---------------------------------



More information about the Baypiggies mailing list