Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a set of letters from a string

Tags:

postgresql

I want to remove the vowels from the email id. Which function should I use? I am trying to find the difference between translate and replace in postgresql but didn't get the exact difference

like image 715
user2589918 Avatar asked Jul 17 '13 05:07

user2589918


People also ask

How do I remove a specific number of characters in a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do I remove certain letters from a string in Python?

Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .

How do you remove part of a string?

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.


2 Answers

translate() replaces a set of single characters (passed as a string) with another set of characters (also passed as a string), for example:

translate('abcdef', 'ace', 'XYZ') --> 'XbYdZf'

replace() replaces occurrences of a string of arbitrary length with another string:

replace('abcdef', 'bc', 'FOO') --> 'aFOOdef'
like image 151
Bohemian Avatar answered Oct 18 '22 23:10

Bohemian


In this case you probably actually want regexp_replace.

Assuming by "vowel" you mean "Western European (English) language vowel letters" you might write:

SELECT regexp_replace('[email protected]', '[aeiou]', '', 'gi');

the gi in the fourth argument says "apply this regular expression globally to the whole input string not just to the first match, and make it case insensitive".

Remember that w and y are sometimes vowel-sounds, depending on their context, too. You won't be able to handle that with a regexp so it depends on whether or not you care for this purpose.

You're less likely to need to deal with other character sets if you're working with email addresses so a regexp might be OK for this.

In most cases mangling words with regular expressions would not be a good approach, though; for example, Russian in the Cyrillic alphabet uses A Э У О Ы Я Е Ё Ю И as vowels. Additionally, depending on the language, the same letter in the same script might or might not be a vowel! Keep reading here for more than you ever wanted to know.

like image 15
Craig Ringer Avatar answered Oct 18 '22 23:10

Craig Ringer