Sunday, May 31, 2009

Remove the words having special characters from a string

Lets learn how to remove the words from a string which contains the special characters. I mean the word having non-English characters. Consider the below example.
asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8

You can see several words from the above contains special/non-english character on them. I'll write a regex that will remove those kind of words from the sentence above.

A PHP code:
$string = "asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8";
$string = preg_replace('/\S*[^a-z0-9A-Z\s,\.]+\S*/', '', $string);
print "$string\n";

Output: asdasd 123, asda koskso

A Perl code:
$string = "asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8";
$string =~ s/\S*[^a-z0-9A-Z\s,\.]+\S*//g;
print "$string\n";

Output: asdasd 123, asda koskso

You can use trim on the string, or replace multiple spaces with single one after that. Also you can add other characters like dot, comma inside the regex to distinguish the word.

Cheers!!!

1 comment:

Servay said...

can show how to remove special character in mysql

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debug...