473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7477
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.GetEnc oding(1251).Get Bytes(s); // 8 bit characters
string t = Encoding.ASCII. GetString(b); // 7 bit characters

t == aaaaaaeeeeiiiio ooooouuuuyy

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

"Morten Wennevik" <Mo************ @hotmail.com> wrote in message news:opr9k4y6o1 klbvpo@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.GetEnc oding(1251).Get Bytes(s); // 8 bit characters
string t = Encoding.ASCII. GetString(b); // 7 bit characters

t == aaaaaaeeeeiiiio ooooouuuuyy

--
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
9041
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, "", $input); result: "tring1"
10
39354
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: domything() And the regexp search assuming no case restriction would be,
3
3331
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 which is in paragraph form, but has line breaks within the paragraphs. I need to do as much processing as possible in order to minimise the amount of manual changes that I have to make. Sample text is as follows:
19
3577
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 simulate javscript access to stylesheets in Opera. I thought I had it licked untill the '/' characters in url('') tripped me up Below is a test case. I've tried many things. but if I can't figure out a nice clean RegExp(), I'm going to have to...
4
3456
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 "PRACTICO". What's the best way to do this? TIA,
4
2751
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 re module in python, since this one takes the regexp as if were specified this way: '^<widget class=".*"
23
2877
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 is not valid. Here's my attempt, neither of my regexps work quite how I want: import os import re
3
1623
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 getting these strings into the RegExp object. The RegExp constructor seems to want two parameters - the non / delimited expression, and the global modifiers.
5
2143
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 like the effect of () but without having to have many occurrences of .
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.