postgres regexp_replace want to allow only a-z and A-Z - regex

In a table column in string we can have numbers/special chars/white spaces.
I want to replace numbers/special chars/white space with empty char, i see there is function named regexp_replace but how to use not much user friendly help avaialble for example i want to use following string.
String = 'abc$wanto&toremove#special~chars'
I want to remove all special chars and numbers from above string want to allow only a-z and A-Z rest of chars should be replaced with '' how to do that ?

SELECT regexp_replace('abc$wanto&toremove#special~chars', '[^a-zA-Z]', '', 'g');
regexp_replace
------------------------------
abcwantotoremovespecialchars

For me the following worked.
regexp_replace(code, '[^a-zA-Z0-9]+', '','g')
As it adds global filter so it repeats the regex for the entire string.
Example,
SELECT regexp_replace('Well- This Did-Not work&*($%%)_', '[^a-zA-Z0-9]+', '')
Returns: "WellThis Did-Not work&*($%%)_"
SELECT regexp_replace('Well- This Did-Not work&*($%%)_', '[^a-zA-Z0-9]+', '','g')
Returns: "WellThisDidNotwork"
Which has the characters we don't want removed.

To make it simpler:
regexp_replace('abc$wanto&toremove#special~chars', '[^[:alpha:]]')

If you want to replace the char with the closest not special char, you can do something like this:
select
translate(
lower( name ), ' ''àáâãäéèëêíìïîóòõöôúùüûçÇ', '--aaaaaeeeeiiiiooooouuuucc'
) as new_name,
name
from cities;

Should be:
regexp_replace('abc$wanto&toremove#special~chars', '[^a-zA-Z]+', '')

Related

Oracle: Special characters filter with few exceptions

I need some quick help.
I want to filter the input string and remove special characters except space( ), period(.), comma(,), hyphen(-), ampersand(&) and apostrophe(').
I am using below but it's filtering out everything except period(.) and comma(,).
SELECT REGEXP_REPLACE('*Bruce*-*Martha*-&-*Thomas%* *Wyane''s* *Enterprises* ([#Pvt,Ltd.])', '[^0-9A-Za-z,.'' ]', '')
FROM dual;
Input String: *Bruce*-*Martha*-&-*Thomas%* *Wyane's* *Enterprises* ([#Pvt,Ltd.])
What I am expecting: Bruce-Martha-&-Thomas Wyane's Enterprises Pvt,Ltd.
What I am getting: BruceMarthaThomas Wyane's Enterprises Pvt,Ltd.
Thanks.
You may use
SELECT REGEXP_REPLACE('*Bruce*-*Martha*-&-*Thomas%* *Wyane''s* *Enterprises* ([#Pvt,Ltd.])', '[^&0-9A-Za-z,.'' -]+', '') FROM dual
See the regex demo
The [^&0-9A-Za-z,.'' -]+ pattern will match one or more occurrences of any char but &, ASCII letter, digit, comma, dot, single apostrophe, space and hyphen.
To support any whitespace, replace the literal space with [:space:]:
'[^&0-9A-Za-z,.''[:space:]-]+'

How do I remove all characters that aren't alphabetic from a string in PL/SQL?

I have a PL/SQL procedure and I need to take a string and remove all characters that aren't alphabetic. I've seen some examples and read documentation about the REGEXP_REPLACE function but can't understand how it functions.
This is not a duplicate because I need to remove punctuation, not numbers.
Either:
select regexp_replace('1A23B$%C_z1123d', '[^A-Za-z]') from dual;
or:
select regexp_replace('1A23B$%C_z1123d', '[^[:alpha:]]') from dual;
The second one takes into account possible other letters like:
select regexp_replace('123żźć', '[^[:alpha:]]') from dual;
Result:
żźć
Also to answer your question about how the functions works: the first parameter is the source string, the second - a regular expression - everything which will be matched to it, will be replaced by the third argument (optional, NULL by default, meaning all matched characters will just be removed).
Read more about regular expressions:
http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
you can use regexp like that:
SELECT REGEXP_REPLACE(UPPER('xYztu-123-hello'), '[^A-Z]+', '') FROM DUAL;
also answered here for non-numeric chars
Try this:
SELECT REGEXP_REPLACE('AB$%c','[^a-zA-Z]', '') FROM DUAL;
Or
SELECT REGEXP_REPLACE( your_column, '[^a-zA-Z]', '' ) FROM your_table;
Read here for more information

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]

Regex in Oracle PL/SQL to remove unwanted characters from a string containing a phone number

I need to remove the characters -, +, (, ), and space from a string in Oracle. The other characters in the string will all be numbers.
The function that can do this is REGEXP_REPLACE. I need help writing the correct regex.
Examples:
string '23+(67 -90' should return '236790'
string '123456' should return '123456'
Something like
SQL> ed
Wrote file afiedt.buf
1 with data as (
2 select 'abc123def456' str from dual union all
3 select '23+(67 -90' from dual union all
4 select '123456' from dual
5 )
6 select str,
7 regexp_replace( str, '[^[:digit:]]', null ) just_numbers
8* from data
SQL> /
STR JUST_NUMBERS
------------ --------------------
abc123def456 123456
23+(67 -90 236790
123456 123456
should do it. This will remove any non-digit character from the string.
regexp_replace is an amazing function, but it is a bit difficult.
You can use TRANSLATE function to replace multiple characters within a string. The way TRANSLATE function differs from REPLACE is that, TRANSLATE function provides single character one to one substitution while REPLACE allows you to replace one string with another.
Example:
SELECT TRANSLATE('23+(67 -90', '1-+() ', '1') "Replaced" FROM DUAL;
Output:
236790
In this example, ‘1’ will be replaced with the ‘1’ and ‘-+()‘ will be replaced with null value since we are not providing any corresponding character for it in the ‘to string’.
This statement also answers your question without the use of regexp.
You would think that you could use empty string as the last argument, but that doesn't work because when we pass NULL argument to TRANSLATE function, it returns null and hence we don’t get the desired result.
So I use REPLACE if I need to replace one character, but TRANSLATE if I want to replace multiple characters.
Source: https://decipherinfosys.wordpress.com/2007/11/27/removing-un-wanted-text-from-strings-in-oracle/
search for \D or [\-\+, ]and replace with empty string ''
regexp_replace is an amazing function, save a lot of time to replace alphabets in a alphanumeric string to convert to number.