I have a column named details which has special character as | I want to replace it with - . How can I do it in spark Java?
I have tried:
regex_replace(details ,"|","-")regex_replace(details ,"\\|","-")
Just wanted to know which is correct one or two and what is the use of \\ before the special character. What if I don't include \\, will my | be replaced in the details string?
If you don't include
\\, which is an escape sequence for the regular expression, you will get a-in between every single character of your string. Meaning that if you have:my | text, and you useregexp_replace(details, "|", "-"), you will end up with:-m-y- -|- -t-e-x-t-because|has a special meaning in regular expression (a|b-> alternamte - match eitheraorb).Therefore, you must use your second option which will return
my - text.