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 6.7k times
-2
-
3[Regex101](http://regex101.com/r/yS5fU8/2) offers a clear explanation. – dee-see Jul 11 '14 at 17:00
4 Answers
22
^
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:
![](../../users/profiles/3622940.webp)
Unihedron
- 10,251
- 13
- 53
- 66
-
-
@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
2
^
- start of a line.(\d{1,2})
- captures upto two digits(ie; one or two digits).$
- End of the line.
![](../../users/profiles/3297613.webp)
Avinash Raj
- 160,498
- 22
- 182
- 229
1
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
![](../../users/profiles/3560174.webp)
Kurn Mogh
- 74
- 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
1
- ^ 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.
![](../../users/profiles/250094.webp)
Alex Dresko
- 4,817
- 2
- 35
- 54
-
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