cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Escaping $ (dollar sign) in a regex backreference in notebook (so not seen as a parameter)

eriodega
Contributor

I am trying to do a regular expression replace in a Databricks notebook.

The following query works fine in a regular query (i.e. not running it in a cell in a notebook):

 

 

select regexp_replace('abcd', '^(.+)c(.+)$', '$1_$2') --normally outputs ab_d

 

 

However, if you run it in a notebook cell, it does not work correctly. Instead the query output is an empty string and a small warning is shown:

 

 

SQL query contains $ parameter. Migrate to the parameter marker syntax, :param:
Parameter values:

    2: <empty>
    1_: <empty>

 

 

So it is incorrectly interpreting the back references ($1 and $2) as parameters that are not supplied via a widget.
 
I can work around this issue by doing some string concatenation like so (so that $ is never followed by a number):

 

select regexp_replace('abcd', '^(.+)c(.+)$', '$'||'1_$'||'2')โ€‹

 

but that feels like a hack/kludge. Is there a better way to avoid this issue or escape the dollar sign?
1 ACCEPTED SOLUTION

Accepted Solutions

filipniziol
Contributor

Hi, just put a backslash before $ as an escape character:

filipniziol_0-1724958137217.png

 

View solution in original post

1 REPLY 1

filipniziol
Contributor

Hi, just put a backslash before $ as an escape character:

filipniziol_0-1724958137217.png

 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group