Skip to content

REGEXREPLACE uses $ for groups, and a backslash escapes the next character

Updated

REGEXREPLACE(text, regular_expression, replacement) rewrites matches. In the replacement, $1 means capturing group 1. A backslash escapes the next character: \$ is a literal dollar, and \\ is one literal backslash. RE2’s own \1 group syntax is not what this argument uses. If you write \1 hoping for the group, Sheets inserts the digit 1. No error. A $ group that is not in the pattern is #REF!. Sheets string literals do not eat backslashes the way JavaScript does. The characters between the quotes are what the replacement parser sees, and that parser is the second set of rules.

A pattern that does not match returns the original text, not #N/A. That is REGEXREPLACE miss. Extracting groups is REGEXEXTRACT groups. Keep the Google Sheets UI in English. Sheet Error is an independent formula-error library, not a Google product.

  1. Open a blank spreadsheet. In A1 type raw. In A2 type N12-E7. In A4 enter ="N"&CHAR(92)&"12". A4 displays N\12. CHAR(92) is the backslash. Typing it in the formula bar is easy to miscount; the code point is not.

Groups with $. In C1 type swap. In C2 enter =REGEXREPLACE(A2,"([A-Z]+)(\d+)","$1/$2"). Both letter-digit pairs match. C2 is N/12-E/7. $1 is the letters, $2 is the digits, and the hyphen was not in the match so it stays. That row is the control: dollar group references work.

Backslash-digit is not a group. In C4 enter =REGEXREPLACE(A2,"([A-Z]+)","\1"). The third argument is a backslash and the character 1. The replacement parser treats the backslash as an escape, so the output character is the digit 1. Each letter run becomes 1. C4 is 112-17. No triangle. You asked for group 1 in another engine’s syntax and got a literal digit. Leave C4 as 112-17.

C4 selected. Formula bar shows =REGEXREPLACE(A2,"([A-Z]+)","\1"). C4 is 112-17 with no error triangle. C2 is N/12-E/7 from $1/$2. A2 is N12-E7.

A literal dollar and a literal backslash. In C6 enter =REGEXREPLACE(A2,"([A-Z]+)","\$1"). The replacement text is backslash, dollar, 1. The parser turns \$ into a dollar sign and then writes 1. C6 is $112-$17. In C7 enter =REGEXREPLACE(A2,"\d+","\\"). The pattern "\d+" is a digit run, because the formula string contains backslash + d. The replacement "\\" is two backslash characters; the parser emits one. C7 is N\-E\. Leave C6 and C7 as the literal-character cases.

A group that is not there. In C9 enter =REGEXREPLACE(A2,"([A-Z]+)","$2"). The pattern has group 1 only. $2 is #REF!. Leave C9 broken. This is not a miss. A miss would return N12-E7 unchanged. See REGEXREPLACE miss.

A backslash in the source. In C11 enter =REGEXREPLACE(A4,"\\","/"). The pattern is two backslashes in the formula, which is one literal backslash for RE2. C11 is N/12. In C12 enter =REGEXREPLACE(A4,"\","/"). A single trailing backslash is not a valid pattern. C12 is #REF!. Leave C12 broken. The source cell really does contain a backslash; the pattern string is what failed.

Leave C4 as 112-17. Leave C2 as N/12-E/7. Leave C9 and C12 as #REF!.

  • Two languages are stacked. The Sheets formula string is literal, including backslashes. Then REGEXREPLACE reads $ digits as groups and \ as an escape. "\1" arrives as backslash + 1, and the escape turns that into 1. "\\" arrives as two backslashes, and the escape turns that into one backslash in the cell.
  • $1 is group 1 of each match, not “the whole match plus a 1.” The whole match, when you need it, is $0. A dollar that is not followed by a digit is just a dollar. You only need \$ when a digit follows and you want the dollar printed.
  • $2 with one group is #REF!. Sheets does not leave the characters $2 in the cell. If you needed those characters, escape the dollar.
  • In the pattern, "\d" is the digit class, and "\\" is a literal backslash. A pattern that ends in a single backslash does not compile and is #REF!, same token as C9, different cause. Read the formula before you add another slash.
  • Replacing every match is what this function does. There is no occurrence number. The second hyphen, only, is SUBSTITUTE nth if the text is literal. SUBSTITUTE will also replace a backslash without regex: =SUBSTITUTE(A4,CHAR(92),"/") is N/12.

This is not REGEXEXTRACT groups. Extract returns the group as the cell value. Replace uses $1 inside a new string. A miss on extract is #N/A. A miss on replace is the original text.

This is not FIND vs SEARCH case. Those functions do not take $1.

Leave C4, C9, and C12 broken.

Insert the groups. Keep C2. For a full swap of the two pairs, =REGEXREPLACE(A2,"([A-Z]+)(\d+)-([A-Z]+)(\d+)","$1/$2/$3/$4") returns N/12/E/7. Count the opening parentheses. That count is the highest $ you may use.

C2 selected. Formula bar shows =REGEXREPLACE(A2,"([A-Z]+)(\d+)","$1/$2"). C2 is N/12-E/7 with no error triangle. C4 is still 112-17. C9 is still #REF! from $2. A2 is N12-E7.

Print a dollar or a backslash. C6 replaces each letter run with the two characters $ and 1, so the cell is $112-$17. The 1 is literal. The letters are gone. That is the escape demo, not a capture. To prefix a dollar while keeping the group, concatenate outside the replacement: ="$"&REGEXREPLACE(A2,"([A-Z]+)(\d+)","$1$2") on the first match’s idea, or keep C2 and add the currency in the sheet format. For a backslash in the result, the replacement argument is "\\" as in C7.

A backslash in the input. Keep C11, or use SUBSTITUTE with CHAR(92) so you are not counting slashes in a regex.

Do not wrap C4 in IFERROR. 112-17 is a successful replace of the wrong kind. See IFERROR vs IFNA. IFERROR on C9 would hide a bad group number.

If the cell shows a bare digit where a group should be, you used \1 instead of $1. If it shows $1 as characters, you escaped the dollar or the pattern did not match. If it is #REF!, either the $ number is too high or the pattern’s backslashes do not compile. If the text came back unchanged, the pattern missed; that is the other page.

$1/$2 on N12-E7 produces N/12-E/7. \1 on the letters produces 112-17 and no error. \$1 produces a dollar and a digit. "\\" as the replacement inserts one backslash. $2 with one group is #REF!. A single backslash as the whole pattern is #REF!. Two backslashes in the pattern match one backslash in the cell.