views:

100

answers:

5

Hi! Two quick questions: What would be a RegEx string for three letters and two numbers? I.E "LET 12" ? And so I don't bother you guys in the future, would you happen to know any good RegEx resources/tools?

EDIT: There will be space before and after the 'LET 12', sorry I forgot to mention that! :)

+1  A: 

/[a-z]{3} [0-9]{2}/i will match 3 letters followed by a whitespace character, and then 2 numbers. [a-z] is a character class containing the letters a through z, and the {3} means that you want exactly 3 members of that class. The space character matches a literal space (alternately, you could use \s, which is a "shorthand" character class that matches any whitespace character). The i at the end is a pattern modifier specifying that your pattern is case-insenstive.

If you want the entire string to only be that, you need to anchor it with ^ and $:

/^[a-z]{3} [0-9]{2}$/i

Regular expression resources:

Daniel Vandersluis
Why not `/\w{3} \d{2}/` out of interest?
Ryan Bigg
I was being more verbose for clarity, but `\d` could be used in place of `[0-9]`. Note that `\w` is not equivalent to `[a-z]`, though! It's generally `[A-Za-z0-9_]`.
Daniel Vandersluis
Why was this downvoted?
Daniel Vandersluis
Probably revenge for you downvoting Sudhir.
Stephen
Nope, I got the downvote first and the downvote on Sudhir's answer is because `\w` is wrong. If the person who downvoted would like to tell me what's wrong I'd gladly fix it.
Daniel Vandersluis
In which case, I have no idea. Someone not realising that /i does the case insensitivity for you? Or maybe because `\s` does not, as far as I'm aware, just match spaces but actually all whitespace including newlines etc?
Stephen
To pick nits, the accepted answer uses `\s` as well ;) Ah well, it's just frustrating to get phantom downvotes without explanation.
Daniel Vandersluis
It is indeed. I'd upvote you, but your regexp is technically incorrect :p. Not that the OP was very specific about it, but still. I'm a nit picker ;).
Stephen
@Stephen Better? ;)
Daniel Vandersluis
@Daviel Vandersluis Much :P
Stephen
@Stephen Haha thanks :P
Daniel Vandersluis
Lol! Nope, I don't do revenge, but +1 for teaching me case insensitivity and anchoring.
Sudhir Jonathan
+3  A: 

For a good resource, try this website and the program RegexBuddy. You may even be able to figure out the answer to your question yourself using these sites.

To start you off you want something like this:

/^[a-zA-Z]{3}\s+[0-9]{2}$/

But the exact details depend on your requirements. It's probably a better idea that you learn how to use regular expressions yourself and then write the regular expression instead of just copying the answers here. The small details make a big difference. Examples:

  • What is a "letter"? Just A-Z or also foreign letters? What about lower case?
  • What is a "number"? Just 0-9 or also foreign numerals? Only integers? Only positive integers? Can there be leading zeros?
  • Should there be a single space between the letters and numbers? Or any amount of any whitespace? Even none?
  • Do you want to search for this string in a larger text? Or match a line exactly?
  • etc..

The answers to these questions will change the regular expression. It would be much faster for you in the long run to learn how to create the regular expression than to completely specify your requirements and wait for other people to reply.

I forgot to mention that there will be a space before and after. How do I include that?

Again you need to consider the questions:

  • Do you mean just one space or any amount of spaces? Possibly not always a space but only sometimes?
  • Do you mean literally a space character or any whitespace characters?

My guess is:

/^\s+[a-zA-Z]{3}\s+[0-9]{2}\s+$/
Mark Byers
I'm sorry, totally new to RegEx! Yes there will be a space before and after. I'm sorry I didn't specify
Jon McIntosh
Before and after doesn't make sense! Your example string is 'LET 12'. That has one space, right in the middle! Do you mean you want to match ' LET 12 '? That has three spaces (left, middle, right). Also, you need to specify things like case-sensitivity, like Mark has said.
Stephen
@Mark Byers: +1 You write and explain so clearly. You are a great resource to this site.
drewk
@drewk: Thanks. :)
Mark Byers
A: 

\w{3}\s{1}\d{2} And I like this site.

EDIT:[a-zA-Z]{3}\s{1}\d{2} - The \w supports numeric characters too.

Sudhir Jonathan
-1 for {1}, which does not makes no sense, +1 for using character classes -> 0 in total
unbeli
`\w` is not equivalent to `[a-zA-Z]`, but generally `[a-zA-Z0-9_]`
Daniel Vandersluis
@unbeli, Nothing wrong with {1} ... it's unnecessary for the regexp engine, but it's syntactically and semantically valid. It makes the regexp easier for beginners to understand because all three terms are parallel. @DanielV, good point.
LarsH
@LarsH {1} is junk, does not make it simpler, does not make it easier to understand
unbeli
I removed my downvote since you fixed your answer.
Daniel Vandersluis
+2  A: 

^([A-Za-z]{3}) ([0-9]{2})$ assuming one space between the letters/numbers, as in your example. This will capture the letters and numbers separately.

I use http://gskinner.com/RegExr/ - it allows you to build a regex and test it with your own text.

Richard Fearn
Awesome, but I forgot to mention that there will be a space before and after. How do I include that?
Jon McIntosh
A: 

As you can probably tell from the wide variety of answers, RegEx is a complex subject with a wide variety of opinions and preferences, and often more than one way of doing things. Here's my preferred solution.

^[a-zA-Z]{3}\s*\d{2}$

I used [a-zA-Z] instead of \w because \w sometimes includes underscores.

The \s* is to allow zero or more spaces.

I try to use character classes wherever possible, which is why I went with \d.

John M Gant