473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

RegExp to strip accents while ignoring case

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
}
Nov 16 '05 #1
4 7433
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bosconian | last post by:
Using preg_replace() is there a simple regexp to strip everything from a string except alpha and numeric chars (a-zA-Z0-9)? $input = "$tring1!"; $pattern = $input = preg_replace($pattern, "",...
10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
3
by: MichaelC | last post by:
Hi all. I am having a particularly difficult time with a perl script that I am writing. The problem area is a place where I need to strip some newlines out of a file. My source data is text...
19
by: Dr Clue | last post by:
I'm not really an expert with RegExp() , although I do use it. The problem I have is that I want to strip comments out of a CSS file using RegExp() The reason is that I'm loading and parsing to...
4
by: Jon Maz | last post by:
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...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
3
by: jgarrard | last post by:
Hi, I have an array of strings which are regular expressions in the PERL syntax (ie / / delimeters). I wish to create a RegExp in order to do some useful work, but am stuck for a way of...
5
by: gentsquash | last post by:
In a setting where I can specify only a JS regular expression, but not the JS code that will use it, I seek a regexp component that matches a string of letters, ignoring case. E.g, for "cat" I'd...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.