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 8.5k times
0
-
3[Regex101](http://regex101.com/r/yS5fU8/2) offers a clear explanation. – dee-see Jul 11 '14 at 17:00
4 Answers
25
^Asserts position at start of the string(Denotes the start of a capturing group-
\dNumerical 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,601
- 13
- 59
- 69
-
-
@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
3
^- start of a line.(\d{1,2})- captures upto two digits(ie; one or two digits).$- End of the line.
Avinash Raj
- 166,785
- 24
- 204
- 249
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
- 4,985
- 2
- 36
- 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
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
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