Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove non letters

Tags:

regex

ruby

I'm trying to remove non-letters from a string. Would this do it:

c = o.replace(o.gsub!(/\W+/, ''))
like image 887
newbie_86 Avatar asked Mar 24 '11 19:03

newbie_86


People also ask

How do you remove all non letters from a string?

To remove all non-alphanumeric characters from a string, call the replace() method, passing it a regular expression that matches all non-alphanumeric characters as the first parameter and an empty string as the second. The replace method returns a new string with all matches replaced.

How do you remove non-alphanumeric characters?

Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.

How do you identify non-alphanumeric characters?

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, a total of 62 characters, and we can use regex [a-zA-Z0-9]+ to matches alphanumeric characters.

How do I strip non-alphanumeric characters in C#?

Using Regular Expression We can use the regular expression [^a-zA-Z0-9] to identify non-alphanumeric characters in a string. Replace the regular expression [^a-zA-Z0-9] with [^a-zA-Z0-9 _] to allow spaces and underscore character.


2 Answers

Just gsub! is sufficient:

o.gsub!(/\W+/, '')

Note that gsub! modifies the original o object. Also, if the o does not contain any non-word characters, the result will be nil, so using the return value as the modified string is unreliable.

You probably want this instead:

c = o.gsub(/\W+/, '')
like image 84
Cameron Avatar answered Oct 14 '22 20:10

Cameron


Remove anything that is not a letter:

> " sd  190i.2912390123.aaabbcd".gsub(/[^a-zA-Z]/, '')
"sdiaaabbcd"

EDIT: as ikegami points out, this doesn't take into account accented characters, umlauts, and other similar characters. The solution to this problem will depend on what exactly you are referring to as "not a letter". Also, what your input will be.

like image 24
edmz Avatar answered Oct 14 '22 18:10

edmz