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

Welcome to IWETHEY!

New Re: Regex question
The first one is invalid syntax as the asterisk does not immediately follow a character or character class to repeat (and the parsers I tested don't automatically assume it is a literal * in that case).

PS: The second one will match asterisks as the * is inside the character class []. This may not be what you intended.
New Oops, updated
The board removed the backslash before the asterisk.

This is for a drivers license validation. First five characters are the first five of the last name. If the last name is fewer than five, fill with asterisks. So yes, it should match asterisks.

As I read the first version (with the escaping showing up correctly), it will match five letters or five asterisks. Am I interpreting that right?
--

Drew
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)

If nautical nonsense be what ye wish...
43 ms