[Tutor] need help with a regular expression

Mark Tolonen metolone+gmane at gmail.com
Sat Jun 28 19:32:17 CEST 2008


"Kelie" <kf9150 at gmail.com> wrote in message 
news:loom.20080628T091523-776 at post.gmane.org...
> Hello,
>
> I'm trying to write a regular expression to filter strings that meet the
> following criteria:
>
> 1. Starts with 0-3 underscores;
> 2. Followed by one letter;
> 3. Then followed by 0 or more letters or digits or hyphens('-'),
> 4. Ends with one letter or digit; and
> 5. There should not be more than one continuous hyphens.
>
> This is what I have so far. I think it meets all the criteria except for 
> the
> last one. Haven't figured out the answer yet.
>
> re.compile(r'_{0,3}[A-Z][A-Z0-9\-]*[A-Z0-9]')
>

In rule 4, does the string end with an *additional* letter or digit?  The 
string 'A' would seem to fit the rules, but the example provided would fail.

re.compile(r'^_{0,3}[A-Z](?:[A-Z0-9]|-(?!-))*[A-Z0-9]$')   # if rule 4 is an 
additional letter or digit
re.compile(r'^_{0,3}[A-Z](?:[A-Z0-9]|-(?!-))*(?<!-)$')       # if 
single-letter strings are allowed

-Mark 




More information about the Tutor mailing list