views:

120

answers:

4

How can I write a regex to match strings following these rules?

  • 1 letter followed by 4 letters or numbers, then
  • 5 letters or numbers, then
  • 3 letters or numbers followed by a number and one of the following signs: ! & @ ?

I need to allow input as a 15-character string or as 3 groups of 5 chars separated by one space.

I'm implementing this in JavaScript.

+2  A: 

This should be it:

/^[a-z][a-z0-9]{4} ?[a-z0-9]{5} ?[a-z0-9]{3}[0-9][!&@?]$/i

Feel free to change 0-9 and [0-9] with \d if you see fit.
The regex is simple and readable enough. ^ and $ make sure this is a whole match, so there aren't extra characters before or after the code, and the /i flag allows upper or lower case letters.

Kobi
I wish I had had someone to do all of my homework too...
Stephen
The specification does only allow 15 or 17 characters; your regular expression would also allow 16 characters.
Gumbo
@Stephen - i wish i had homework.
Sky Sanders
@Gumbo - that is correct, and I was well aware of that. Seems nicer that way. @Stephen - I wish people stopped adding the homework tag for no good reason. Lets see what gets implemented first.
Kobi
+1  A: 

I would start with a tutorial.

Pay attention to the quantifiers (like {N}) and character classes (like [a-zA-Z])

Stephen
+5  A: 

I'm not going to write out the whole regex for you since this is homework, but here are some hints which should help you out:

  • Use character classes. [A-Z] matches all uppercase. [a-z] matches all lowercase. [0-9] matches numbers. You can combine them like so [A-Za-z0-9].
  • Use quantifiers like {n} so [A-Z]{3} gives you 3 uppercase letters.
  • You can put other characters in character classes. Let's say you wanted to match % or @ or #, you could do [%@#] which would match any of those characters.
  • Some meta-characters (characters which have special meaning in the context of regular expressions) will need to be escaped like so: \$ (since $ matches the end of a line)
  • ^ and $ match the beginning and end of the line respectively.
  • \s matches white-space, but if you sanitize your input, you shouldn't need to use this.
  • Flags after the regex do special things. For example in /[a-z]/i, the i ignores case.
Vivin Paliath
It's not clear this is homework, that was just some drive-by tag vandal.
Roger Pate
@Roger: even if it is not homework *per se* the OP clearly finds herself *learning* regular expressions and will get more out of a pedagogical response than a finished answer. Or at least that's my view.
dmckee
@dmckee: I'm not suggesting a pedagogical answer shouldn't be given; just as for non-homework questions, explaining why is always more valuable. But answers along the lines of "you don't deserve a complete answer, here's some hints" are mildly offensive (at best).
Roger Pate
@Roger, I wasn't trying to be offensive. I honestly thought it was homework (I looked at the tag). It may have been drive-by-tagging. But I made the answer assuming that it was homework. And based on my assumption, I think it's better to point people towards the right direction rather than just giving them the answer. I wasn't trying to imply that they didn't deserve an answer. I was just trying to help them learn because I (possibly wrongly) assumed that it was homework; this is what I've personally experienced when asking for help with homework questions (outside SO, when I was in school).
Vivin Paliath
@Vivin: Didn't downvote you or anything, I think you did a decent job outlining various features---and that's the key issue: it's *always* better to point people in the right direction with a solid foundation regardless of whether it's homework or not. I just don't see why answers should change depending on that factor. Homework should not be a riddle students are made to **guess** at.
Roger Pate
A: 
^[a-zA-Z][a-zA-Z0-9]{4} ?[a-zA-Z0-9]{5} ?[a-zA-Z0-9]{3}[\!\&\@\?]$
John
Gumbo