274

I am trying to write a regular expression that will only allow lowercase letters and up to 10 characters. What I have so far looks like this:

pattern: /^[a-z]{0,10}+$/ 

This does not work or compile. I had a working one that would just allow lowercase letters which was this:

pattern: /^[a-z]+$/ 

But I need to limit the number of characters to 10.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 7
    The {} and the + do the same thing (counting), thus they are redundant, hence the error. – PhiLho Oct 30 '09 at 12:36
  • 3
    You should drop the '+' from /^[a-z]{0,10}+$/. It should be /^[a-z]{0,10}$/ – Rashmi Pandit Oct 30 '09 at 12:37
  • 2
    Note that pattern `^[a-z]{0,10}+$` compiles on some languages and some of the notable ones are Java, Perl (5.10+), PHP, Ruby (1.9+). See [this page](http://www.regular-expressions.info/possessive.html) for more detail. – Jerry Jan 07 '14 at 06:59
  • See also universal solution via lookahead: [Restricting Character length in Regular expression](https://stackoverflow.com/questions/32477182/restricting-character-length-in-regular-expression) – Vadzim Mar 30 '20 at 15:18

6 Answers6

496

You can use curly braces to control the number of occurrences. For example, this means 0 to 10:

/^[a-z]{0,10}$/

The options are:

  • {3} Exactly 3 occurrences;
  • {6,} At least 6 occurrences;
  • {,4} At most 4 occurrences;
  • {2,5} 2 to 5 occurrences.

See the regular expression reference.

Your expression had a + after the closing curly brace, hence the error.

Luatic
  • 8,513
  • 2
  • 13
  • 34
cletus
  • 616,129
  • 168
  • 910
  • 942
  • In chrome dev tools with ember I had to ommit the beginning `^` to property match ONLY requests with a name like `1234` or `5678`, and I had to include the $ sign – Devin Rhode Jun 07 '18 at 22:30
  • 2
    Note: be sure not to add a space between `,` and `10` (`{0, 10}` will not work). – Niek Nov 27 '20 at 10:31
  • I raised a similar question (chars and digits, limited to 6), https://stackoverflow.com/q/76370865/10655742, the question was answered by using the boundery flag \b so you need \b[a-z]{0,10}\b, the suggestion(s) with `^` and `$` did not work for me – KargWare Jun 01 '23 at 06:26
13

/^[a-z]{0,10}$/ should work. /^[a-z]{1,10}$/ if you want to match at least one character, like /^[a-z]+$/ does.

Joren
  • 14,472
  • 3
  • 50
  • 54
11

It might be beneficial to add greedy matching to the end of the string, so you can accept strings > than 10 and the regex will only return up to the first 10 chars. /^[a-z0-9]{0,10}$?/

jfarrell
  • 4,480
  • 3
  • 21
  • 14
10
grep '^[0-9]\{1,16\}' | wc -l

Gives the counts with exact match count with limit

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user6911841
  • 101
  • 1
  • 2
9

It very much depend on the program you're using. Different programs (Emacs, vi, sed, and Perl) use slightly different regular expressions. In this case, I'd say that in the first pattern, the last "+" should be removed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
8
pattern: /[\w\W]{1,10}/g

I used this expression for my case, it includes all the characters available in the text.

Rochdi Belhirch
  • 428
  • 4
  • 6