最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RegEx string for three letters and two numbers with pre- and post- spaces - Stack Overflow

programmeradmin2浏览0评论

Two quick questions:

  1. What would be a RegEx string for three letters and two numbers with space before and after them (i.e. " LET 12 ")?
  2. Would you happen to know any good RegEx resources/tools?

Two quick questions:

  1. What would be a RegEx string for three letters and two numbers with space before and after them (i.e. " LET 12 ")?
  2. Would you happen to know any good RegEx resources/tools?
Share Improve this question edited Jan 18, 2012 at 17:16 julianfperez 1,7445 gold badges40 silver badges69 bronze badges asked Aug 20, 2010 at 20:12 Jon McIntoshJon McIntosh 1
  • Did you want it to match the space between LET and 12? – John M Gant Commented Aug 20, 2010 at 20:16
Add a comment  | 

6 Answers 6

Reset to default 10

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+$/

/[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:

  • http://www.regular-expressions.info - great tutorial with a lot of information
  • http://rexv.org/ - online regular expression tester that supports a variety of engines.

^([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.

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.

\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.

try this regularexpression

[^"\r\n]{3,}
发布评论

评论列表(0)

  1. 暂无评论