IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New No, it will match mixtures
Which isn't what you want either.

The problem is that **AND or *A*N* or AND** will all match both of the regexes you have there, whereas from your description you only want the last one.

Also, put the parens around the {} counts or you'll only get the first character in the match for that group.


>>> import re
>>> parrot = re.compile('^([A-z*]{5})([A-z*]){2}[0-9]{3}([A-z0-9]){2}$')
>>> parrot.match("AN*D*az898zz").groups()
('AN*D*', 'z', 'z')
>>> parrot = re.compile('^([A-z]{5}|[A-z]{4}\*|[A-z]{3}\*\*|[A-z]{2}\*\*\*)([A-z*]{2})[0-9]{3}([A-z0-9]{2})$')
>>> parrot.match("AN*D*az898zz").groups()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
>>> parrot.match("AND**az898zz").groups()
('AND**', 'az', 'zz')


Note I only "fixed" the first match group. The second is left as an exercise to the reader.

Also note that I didn't allow for a single letter with 4 asterisks, or 5 asterisks, under the (possibly erroneous) assumption that you won't be getting people with last names of a single or no letters. Adjust as necessary.

Also also note there may be a better way to do this.

Enjoi.
Regards,
-scott
Welcome to Rivendell, Mr. Anderson.
Expand Edited by malraux April 18, 2012, 11:32:58 PM EDT
New This is why I hate rewriting crap
It leads me into assumptions that aren't true. I don't need to match groups, all I care about is whether the whole string matches the rules or not. The old regex was grouping so it could do the "or" logic with the pipe. Take out the pipe and I don't need the parens.

What I want is:
^([A-z]{5}|[A-z]{4}\*|[A-z]{3}\*\*|[A-z]{2}\*\*\*)[A-z][A-z*][0-9]{3}[A-z0-9]{2}$

Can't wait to see what RegexBuddy says about this one.
--

Drew
     Regex question - (drook) - (7)
         Re: Regex question - (altmann) - (3)
             Oops, updated - (drook) - (2)
                 No, it will match mixtures - (malraux) - (1)
                     This is why I hate rewriting crap - (drook)
         best $40 I ever spent - (boxley) - (2)
             Duh, I've got that, will check tomorrow -NT - (drook) - (1)
                 Also, bunch of others - (crazy)

To Imagine, To Ponder.. perhaps occasionally, To Think.
109 ms