473,856 Members | 1,710 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 "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
4 7482
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
9046
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
39367
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
3334
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
3592
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
3459
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 "prctico" comes out as "practico" - but ignoring case, so that "PRCTICO" comes out as "PRACTICO". What's the best way to do this? TIA,
4
2758
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
2889
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
1628
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
2150
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
9916
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9762
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10696
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10782
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
10384
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...
1
7932
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
7093
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.