By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

RegExp to strip accents while ignoring case

P: n/a
Hi All,

I want to strip the accents off characters in a string so that, for example,
the (Spanish) word "prctico" comes out as "practico" - but ignoring case,
so that "PRCTICO" 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
}
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
Nov 16 '05 #2

P: n/a

"Morten Wennevik" <Mo************@hotmail.com> wrote in message news:opr9k4y6o1klbvpo@morten_x.edunord...
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]


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
Nov 16 '05 #3

P: n/a
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]
Nov 16 '05 #4

P: n/a
Hi,
But for all characters I have tested,
codepage 1251 will convert correctly
except


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

Thanks to all for the help!

JON

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.