-2

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?

user3830267
  • 41
  • 1
  • 1
  • 1

4 Answers4

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:

vis

Unihedron
  • 10,251
  • 13
  • 53
  • 66
2
  • ^ - start of a line.
  • (\d{1,2}) - captures upto two digits(ie; one or two digits).
  • $ - End of the line.
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

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.

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
  • 1
    The "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