Redshift Translate command to replace characters - replace

I need to translate commas in a column to pipe with with spaces on each side in Redshift ('a,b,c' becomes 'a | b | c' using Translate. Something in this statement is not giving me my desired results and I can't figure out why?
select 'a,b,c' as comma_string, translate(comma_string, ',', ' | ' ) as pipe_string
is yielding 'a b c' with no pipes. Having trouble getting the space before and after the pipe as
select 'a,b,c' as comma_string, translate(comma_string, ',', '|' ) as pipe_string
gives me 'a|b|c'

The REPLACE command works for this. NOt sure why Translate doesn't.
select 'a,b,c' as comma_string, REPLACE(comma_string, ',' ,' | ') as pipe_string
yields the desired result 'a | b | c'

You would need to use REPLACE since TRANSLATE only maps single characters:
TRANSLATE is similar to the REPLACE function and the REGEXP_REPLACE function, except that REPLACE substitutes one entire string with another string and REGEXP_REPLACE lets you search a string for a regular expression pattern, while TRANSLATE makes multiple single-character substitutions.
https://docs.aws.amazon.com/redshift/latest/dg/r_TRANSLATE.html

Related

Hive regexp_replace failed to replace backslash

I have a table with a single column name_string, which contains backslash character. I wanted to remove the backslash character using regexp_replace, but it does not work.
Table:
create table t (name_string varchar(100));
insert into table t values ('\\"aaa\\"'), ('\\"bbb\\"');
Query:
select
name_string, regexp_replace(name_string, '\\"', '"')
from t;
returning
+--------------+----------+
| name_string | _c1 |
+--------------+----------+
| \"aaa\" | \"aaa\" |
| \"bbb\" | \"bbb\" |
+--------------+----------+
However, select regexp_replace('\"aaa\"', '\\"', '"') returns the correct result.
I am confused about why this may be the case. Could someone please shed light on this? Appreciate it!
Use 4 backslashes:
select regexp_replace(name_string,'\\\\"','"') from t;
Only backslash needs escaping. In Java and in regex the backslash has special meaning and needs escaping.
Maybe try:
select
name_string, regexp_replace(name_string, '\\\"', '"')
from t;
I think it's about escaping - you escape 2 characters- backslash and double quote

Regular expression exclusion in PostgreSQL

I have to split some string in PostgreSQL on ',' but not on '\,' (backslash is escape character).
For example, regexp_split_to_array('123,45,67\,89', ???) must split the string to array {123, 45, "67\,89"}.
What done already: E'(?<!3),' works with '3' as escape character. But how can I use the backslash instead of 3?
Does not work:
E'(?<!\),' does not split the string at all
E'(?<!\\),' throws error "parentheses () not balanced"
E'(?<!\ ),' (with space) splits on all ',' including '\,'
E'(?<!\\ ),' (with space) splits on all ',' too.
The letter E in front of the text means C string and then you must escape twice, one for the C string and one for the regexp.
Try with and without E:
regexp_split_to_array('123,45,67\,89', '(?<!\\),')
regexp_split_to_array('123,45,67\,89', E'(?<!\\\\),')
Here http://rextester.com/VEE84838 a running example (unnest() is just for row by row display of results):
select unnest(regexp_split_to_array('123,45,67\,89', '(?<!\\),'));
select unnest(regexp_split_to_array('123,45,67\,89', E'(?<!\\\\),'));
You can also split it to groups first:
(\d+),(\d+\,\d+)?
( and later on concatenate them with comma)

regex replace space between words in string

Say we have the strings on the left and we want to replace empty space between words with <->
" Power Lines" => " Power<->Lines"
Even further, can regex also remove spaces such as a trim in the same regex?
" Power Lines" => "Power<->Lines"
These questions pertain to postgres regex_replace function
Easier than a regex you can do:
SELECT replace(trim(both ' ' from ' Power Lines'), ' ', '<->');
+---------------+
| replace |
|---------------|
| Power<->Lines |
+---------------+
SELECT 1
Time: 0.003s
If you want to do it with a Regex, the syntax is regexp_replace(string text, pattern text, replacement text [, flags text]) (see https://www.postgresql.org/docs/current/static/functions-string.html)

Teradata regexp_replace to eliminate specific special characters

I imported a file that contains email addresses (email_source). I need to join this table to another, using this field but it contains commas (,) and double quotes (") before and after the email address (eg. "johnsmith#gmail.com,","). I want to replace all commas and double quotes with a space.
What is the correct syntax in teradata?
Just do this:
REGEXP_REPLACE(email_source, '[,"]', ' ',1,0,i)
Breakdown:
REGEXP_REPLACE(email_source, -- sourcestring
'[,"]', -- regexp
' ', --replacestring
1, --startposition
0, -- occurrence, 0 = all
'i' -- match -> case insensitive
)
You don't need a regex for this, a simple oTranslate should be more efficient:
oTranslate(email_source, ',"', ' ')

Need to form pattern for regexp_replace

I have input string something like :
1.2.3.4_abc_4.2.1.44_1.3.4.23
100.11.11.22_xyz-abd_10.2.1.2_12.2.3.4
100.11.11.22_xyz_123_10.2.1.2_1.2.3.4
I have to replace the first string formed between two ipaddress which are separated by _, however in some string the _ is part of the replacement string (xyz_123)
I have to find the abc, xyz-abd and xyz_123 from the above string, so that I can replace with another column in that table.
_.*?_(?=\d+\.)
matches _abc_, _xyz-abd_ and _xyz_123_ in your examples. Is this working for you?
DECLARE
result VARCHAR2(255);
BEGIN
result := REGEXP_REPLACE(subject, $$_.*?_(?=\d+\.)$$, $$_foo_$$);
END;
Probably this is enough:
_[^.]+_
and replace with
_Replacement_
See it here on Regexr.
[^.]+ uses a negated character class to match a sequence of at least one (the + quantifier) non "." characters.
I am also matching a leading and a trailing "_", so you have to put it in again in the replacement string.
If PostgreSQL supports lookbehind and lookahead assertions, then it is possible to avoid the "_" in the replacement string:
(?<=_)[^.]+(?=_)
See it on Regexr
In order to map match first two "" , as #stema and #Tim Pietzcker mentioned the regex works. Then in order to append "" to the column , which is what I was struggling with, can be done with || operator as eg below
update table1 set column1=regexp_replace(column1,'.*?(?=\d+.)','' || column2 || '_')
Then for using the another table for update query , the below eg can be helpfull
update table1 as t set column1=regexp_replace(column1,'.*?(?=\d+.)','' || column2 || '_') from table2 as t2 where t.id=t2.id [other criteria]