Connecting Tech Pros Worldwide Forums | Help | Site Map

RegExp to strip accents while ignoring case

Jon Maz
Guest
 
Posts: n/a
#1: Nov 18 '05
Hi All,

I want to strip the accents off characters in a string so that, for example,
the (Spanish) word "práctico" comes out as "practico" - but ignoring case,
so that "PRÁCTICO" comes out as "PRACTICO".

What's the best way to do this?

TIA,

JON

--------------------------------------------------

PS First posted to aspmessageboard -
http://www.aspmessageboard.com/forum...05936&F=34&P=1 -
no answers yet

PPS The Javascript function that I'm porting to C# looks like this:

function quitaAcentos(a) {
re=new RegExp("á", "gi")
a=a.replace(re, "A")
re=new RegExp("´é", "gi")
a=a.replace(re, "E")
re=new RegExp("í", "gi")
a=a.replace(re, "I")
re=new RegExp("ó", "gi")
a=a.replace(re, "O")
re=new RegExp("ú", "gi")
a=a.replace(re, "U")
re=new RegExp("à", "gi")
a=a.replace(re, "A")
re=new RegExp("è", "gi")
a=a.replace(re, "E")
re=new RegExp("é", "gi")
a=a.replace(re, "E")
re=new RegExp("ì", "gi")
a=a.replace(re, "I")
re=new RegExp("ò", "gi")
a=a.replace(re, "O")
re=new RegExp("ó", "gi")
a=a.replace(re, "O")
re=new RegExp("ù", "gi")
a=a.replace(re, "U")
re=new RegExp("â", "gi")
a=a.replace(re, "A")
re=new RegExp("´ê", "gi")
a=a.replace(re, "E")
re=new RegExp("î", "gi")
a=a.replace(re, "I")
re=new RegExp("ô", "gi")
a=a.replace(re, "O")
re=new RegExp("û", "gi")
a=a.replace(re, "U")
re=new RegExp("ä", "gi")
a=a.replace(re, "A")
re=new RegExp("´ë", "gi")
a=a.replace(re, "E")
re=new RegExp("ï", "gi")
a=a.replace(re, "I")
re=new RegExp("ö", "gi")
a=a.replace(re, "O")
re=new RegExp("ü", "gi")
a=a.replace(re, "U")
re=new RegExp(" ", "gi")
a=a.replace(re, "")
re=new RegExp("_", "gi")
a=a.replace(re, "")
re=new RegExp("ñ", "gi")
a=a.replace(re, "N")

return a
}



Morten Wennevik
Guest
 
Posts: n/a
#2: Nov 18 '05

re: RegExp to strip accents while ignoring case


Hi Jon,

I have no idea if this works for all your cases, but, what you essentially want is the basic ASCII characters from a string. I believe that accented characters are all in the extended ascii set and just stripping away the most significant bit will leave you with the unaccented basic character. However, this varies with different code pages. But for all characters I have tested, codepage 1251 will convert correctly except æ Æ

string s = "áàäãâåéèëêíìïîóòöõôøúùüûýÿ";
byte[] b = Encoding.GetEncoding(1251).GetBytes(s); // 8 bit characters
string t = Encoding.ASCII.GetString(b); // 7 bit characters

t == aaaaaaeeeeiiiioooooouuuuyy



--
Happy coding!
Morten Wennevik [C# MVP]
Hans Kesting
Guest
 
Posts: n/a
#3: Nov 18 '05

re: RegExp to strip accents while ignoring case



"Morten Wennevik" <MortenWennevik@hotmail.com> wrote in message news:opr9k4y6o1klbvpo@morten_x.edunord...[color=blue]
> Hi Jon,
>
> I have no idea if this works for all your cases, but, what you essentially want is the basic ASCII characters from a string. I[/color]
believe that accented characters are all in the extended ascii set and just stripping away the most significant bit will leave you
with the unaccented basic character. However, this varies with different code pages. But for all characters I have tested,
codepage 1251 will convert correctly except æ Æ[color=blue]
>
> string s = "áàäãâåéèëêíìïîóòöõôøúùüûýÿ";
> byte[] b = Encoding.GetEncoding(1251).GetBytes(s); // 8 bit characters
> string t = Encoding.ASCII.GetString(b); // 7 bit characters
>
> t == aaaaaaeeeeiiiioooooouuuuyy
>
>
>
> --
> Happy coding!
> Morten Wennevik [C# MVP][/color]

Morten,

I have not tried your code, so it could still work. But the reason will then be the
conversion within GetBytes/GetString, not your explanation!

If it is just a matter of "stripping the most significant bit" then that bit can be thought
of to mean "use an accent". But that would mean that there is just one accented "a"
(and clearly there are more).
Or to put it another way: stripping that bit equals "subtract 128" from the character
code. If you start out with different codes (for the various accents) then you can't
end up with just one "a".

Hans Kesting


Morten Wennevik
Guest
 
Posts: n/a
#4: Nov 18 '05

re: RegExp to strip accents while ignoring case


You are correct, in fact, the conversion to 7-bit is entirely irrelevant as the byte array contains the non accented characters. This strikes me as slightly odd as I would expect the byte array to contain the characters in 8-bit, using the 1251 character set.

--
Happy coding!
Morten Wennevik [C# MVP]
Jon Maz
Guest
 
Posts: n/a
#5: Nov 18 '05

re: RegExp to strip accents while ignoring case


Hi,
[color=blue]
>But for all characters I have tested,
>codepage 1251 will convert correctly
>except æ Æ[/color]

Any reason to think there might be some other characters not covered by
Morten's method?

Thanks to all for the help!

JON



Closed Thread


Similar ASP.NET bytes