3

I have a string containing \s\ keyword. Now, I want to replace it with NULL.

select string,REGEXP_REPLACE(string,'\\\s\\','') from test

But unable to replace with the above statement in spark sql

input: \s\help
output: help

want to use regexp_replace

8
  • 2
    (1) What database are you using? (2) NULL is not something to replace in a string. (3) Do you want replace()? Commented Apr 29, 2020 at 19:39
  • i want to replace with a blank..i'm working with sparksql. only regexp_replace is available. Commented Apr 29, 2020 at 19:43
  • Have you tried replacing it with an empty string instead? Commented Apr 29, 2020 at 19:45
  • yes Jake. it is failing. An error occurred when executing the SQL command: select string,REGEXP_REPLACE(string,'\\s\\','') from test. Invalid operation: Incomplete escape sequence found. The error occurred while parsing the regular expression: Commented Apr 29, 2020 at 19:46
  • 1
    In your comment, you have two backslashes after the s, in your question only one. there is a significant difference as in the question you escape your ' and that will definitely break it. Commented Apr 29, 2020 at 19:57

1 Answer 1

5

To replace one \ in the actual string you need to use \\\\ (4 backslashes) in the pattern of the regexep_replace. Please do look at https://stackoverflow.com/a/4025508/9042433 to understand why 4 backslashes are needed to replace just one backslash

So, the required statement would become like below

select name, regexp_replace(name, '\\\\s\\\\', '') from test

Below screenshot has examples for better understanding

Example input and output for reference

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.