I am trying to understand what the regular expression ^(\d{1,2})$ stands for in google sheets. A quick look around the regex sites and in tools left me confused. Can anybody please help?
Asked
Active
Viewed 1.1e+01k times
2
-
3[Regex101](http://regex101.com/r/yS5fU8/2) offers a clear explanation. – dee-see Jul 11 '14 at 17:00
4 Answers
26
^
Asserts position at start of the string(
Denotes the start of a capturing group-
\d
Numerical digit, 0, 1, 2, ... 9. Etc. -
{1,2}
one to two times. )
You guessed it - Closes the group.$
Assert position at end of the string
Regular expression visualization:

Unihedron
- 10,902
- 13
- 62
- 72
-
-
@AlexDresko [Debuggex regex tool.](https://www.debuggex.com/) Handy! (See button: Embed on StackOverflow) As for the explanation... I wrote it myself. – Unihedron Jul 11 '14 at 17:04
4
^
- start of a line.(\d{1,2})
- captures upto two digits(ie; one or two digits).$
- End of the line.

Avinash Raj
- 172,303
- 28
- 230
- 274
2
It means at least one at most two digits \d{1,2}
, no other characters at the beginning ^
or the end $
. Parenthesis essentially picks the string in it i.e. what ever the digits are

Kurn Mogh
- 84
- 3
-
Parenthesis actually denotes a capturing group, not that they are part of the syntax itself. So `/^\d{1,2}$/` eq. `/^(\d{1,2})$/` – Unihedron Jul 11 '14 at 17:06
2
- ^ matches the start of the line
- The parens can be ignored for now..
- \d{1, 2} means one or two digits
- $ is the end of the line.
The parens, if you need them, can be used to retrieve the digit(s) that were found in the regex.

Alex Dresko
- 5,179
- 3
- 37
- 57
-
1`The parens can be ignored for now..` this one is totally wrong. In regex `()` called capturing groups. Any characters within the parenthesis are captured for later back-referencing. – Avinash Raj Jul 11 '14 at 17:13
-
1The "for now" part of my answer indicates that I was trying not to make that an important part of the explanation _yet_. The OP didn't say anything about needing to use the group so I didn't want to emphasise that detail _yet_. But the last thing I did in my answer was to explain, roughly, what the parens (grouping) are for. – Alex Dresko Jul 11 '14 at 18:05