@PiPeep: I loved that one when it came out :P @seth: Thats what I was thinking, thats why I didn't post a regex in my answer :)
–
MitMaroJul 8 '09 at 19:06

1

Examples vs description ... I think the description (specification) is much better then a number of examples. It tells exactly what is required and leaves (almost) no room for guessing. If there were a number of examples, everyone would ask "Are there always 3 digits?", "How many spaces can be there?", and for badly choosen examples "All your examples have a 3 as second digit of the pin - is this a requirement?". So I have to say, probably the best description of the exspected behavior of a regular expression I have seen on SO up to now.
–
Daniel BrücknerJul 8 '09 at 20:00

^ Anchor start of line.
(?:[1-5] )? Optional access level 1 to 5 followed by a single space,
the group is non-capturing.
[a-z]{3,5} User name with 3 to 5 lower case letters followed by
[0-9]{3,5} 3 to 5 digits.
_+ At least one space.
([0-9]{4}) A 4 digit pin number captured into group \1. Without the
non-capturing group from above the pin number would be
captured into group \2.
_+ At least one space.
\1 A backreference to the pin number captured in group \1.
$ Anchor end of line.

The input must start with a space if no access levvel is present - I assume this is not the desired behavior. Further you don't verify that both pin numbers are equal.
–
Daniel BrücknerJul 8 '09 at 19:45

And \s* makes the space optional while at least one space is required, hence it should be \s+.
–
Daniel BrücknerJul 8 '09 at 19:52

I think the access level together with the space are optional - this expression forces starting with a space if no access level is present, hence ([0-5]_)? instead of ([0-5])?_ or [0-5]?_.
–
Daniel BrücknerJul 8 '09 at 19:39

I think the access level together with the space are optional - this expression forces starting with a space if no access level is present, hence ([0-5]_)? instead of ([0-5])?_ or [0-5]?_.
–
Daniel BrücknerJul 8 '09 at 19:43

Upon re-reading, I think you must be right. I've edited to fix, but your answer looks better anyway. Thanks!
–
Chris NielsenJul 8 '09 at 20:29

What you are describing is not a regular language, therefore it cannot be parsed by a regular expression. The problem is your requirements 6 and 8: regular expressions don't have memory, therefore it is impossible to check that the PINs in 6 and 8 are identical.

@Pavel: Joerg is theoretically correct. What any practically useful package implements includes expressions that are not "regular" in the theoretical sense e.g. they use capturing groups and back references to the same. Most folk use "regular expression" in a rather loose sense, and most of the minority don't get agitated about it ;-)
–
John MachinJul 9 '09 at 0:05

There is no such thing as a "backreference" in regular expressions. You might be thinking of Regexps, which sometimes do have backreferences (and sometimes don't). But the OP was specifically asking about a "regular expression" and not about a Regexp. Those two are totally different things. In particular, Regexps are typically much more powerful than regular expressions.
–
Jörg W MittagJul 9 '09 at 8:33

@Joerg: The OP said "regular expression", but like 99.99% of OPs and answerers he really meant "Regexps as implemented in some particular programming language or package", and would not know (and doesn't need to know) what a "regular language" is in your lexicon. Get used to it.
–
John MachinJul 9 '09 at 15:28