24

I am trying to understand the following regex, I understand the initial part but I'm not able to figure out what {3,19} is doing here:

/[A-Z][A-Za-z0-9\s]{3,19}$/
oguz ismail
  • 34,491
  • 11
  • 33
  • 56
sagar
  • 1,236
  • 1
  • 11
  • 18
  • 5
    Means length between 3 and 19 – David Starkey Jun 10 '13 at 21:10
  • 1
    If you want to learn more. Click the [tag:regex] tag and click 'learn more` – Shiplu Mokaddim Jun 10 '13 at 21:13
  • http://www.regular-expressions.info/repeat.html – Felix Kling Jun 10 '13 at 21:14
  • *related*: http://stackoverflow.com/q/6939517/218196 – Felix Kling Jun 10 '13 at 21:18
  • Note that `A-Za-z0-9` are the same as `\w`, except that `\w` also includes underscore, which people usually want allowed also, anyway, or don't mind having included because it makes it easier to read. So just as a comment, this is better: `/[A-Z][\w\s]{3,19}$/`. This is a word or sentence (including digits) that must start with an uppercase letter, and must be followed by Three to Nineteen characters that can be any combination of word characters or spaces. – Suamere Jun 10 '13 at 21:28

6 Answers6

33

That is the custom repetition operation known as the Quantifier.

\d{3} will find exactly three digits.

[a-c]{1,3} will find any occurrance of a, b, or c at least one time, but up to three times.

\w{0,1} means a word character will be optionally found. This is the same as placing a Question Mark, e.g.: \w?

(\d\w){1,} will find any combination of a Digit followed by a Word Character at least One time, but up to infinite times. So it would match 1k1k2k4k1k5j2j9k4h1k5k This is the same as a Plus symbol, e.g.: (\d\w)+

b{0,}\d will optionally find the letter b followed by a digit, but could also match infinite letter b's followed by a digit. So it will match 5, b5, or even bbbbbbb5. This is the same as an Asterisk. e.g.: b*\d

Quantifiers

Suamere
  • 4,460
  • 1
  • 37
  • 47
8

They are 'quantifiers' - it means 'match previous pattern between 3 and 19 times'

When you are learning regular expressions, it's really use to play with them in an interactive tool which can highlight the matches. I've always liked a tool called Regex Coach, but it is Windows only. Plenty of online tools though - have a play with your regex here, for example.

Paul Dixon
  • 277,937
  • 48
  • 303
  • 335
  • So does this means for example in long string 'abcefghABCdsadgdgsdfsaa' this will try to match patter starting from index 3 and till index 19 – sagar Jun 10 '13 at 21:16
  • No - the pattern will start at the first capital letter, then will match from 3 to 19 alphanumerics or spaces - as it is greedy, it will match as many as it can, but it must match at least 3 – Paul Dixon Jun 10 '13 at 21:17
  • 1
    http://regexpal.com/?flags=g&regex=%5BA-Z%5D%5BA-Za-z0-9%5Cs%5D%7B3%2C19%7D%24&input=abcefghABCdsadgdgsdfsaa – Paul Dixon Jun 10 '13 at 21:19
  • @sagar: `{n,m}` is similar to `?`, `*` or `+` (it is a quantifier) but instead of matching only once, zero or more, or once or more, it matches at least `n` times and at most `m` times. Have a look at the documentation Paul linked to. Also have a look at http://www.regular-expressions.info/repeat.html. – Felix Kling Jun 10 '13 at 21:20
6

{n,m} means "repeat the previous element at least n times and at most m times", so the expression
[A-Za-z0-9\s]{3,19} means "match between 3 and 19 characters that are letters, digits, or whitespace". Note that repetition is greedy by default, so this will try to match as many characters as possible within that range (this doesn't come into play here, since the end of line anchor makes it so that there is really only one possibility for each match).

Andrew Clark
  • 180,281
  • 26
  • 249
  • 286
3

The regular expression you have there /[A-Z][A-Za-z0-9\s]{3,19}$/ breaks up to mean this:

[A-Z] We are looking for a Capital letter

Followed by

[A-Za-z0-9\s]{3,19} a series of letters, digits, or white space that is between 3 and 19 characters

$ Then the end of the line.

David Starkey
  • 1,733
  • 3
  • 28
  • 46
1

It will have to match [A-Za-z0-9\s] between 3 and 19 times.

Here's a good regex reference guide:

http://www.regular-expressions.info/reference.html

immulatin
  • 2,078
  • 1
  • 9
  • 13
0

what does comma separated numbers in curly brace at the end of regex means

It denotes Quantifier with the range specified in curly brace.

curly brace analogues to function with arguments. Where we can specify single integer or two integers which acts as a range between the two numbers.

/[A-Z][A-Za-z0-9\s]{3,19}$/

Using online regex websites we can get understand as follows:

https://regex101.com/

https://regex101.com/

Premraj
  • 56,385
  • 22
  • 212
  • 157