用等效替换特殊字符

时间:2012-10-07 18:31:25

标签: string matlab replace diacritics

如何用等效字符替换以下特殊字符?

元音:AEIOUaeiou分别为ÁÉÍÓÚáéíóú。并且字母Ñ由N。

表达式:

str = regexprep(str,'[^a-zA-Z]','');

将删除非字母表中的所有字符,但如何替换上面显示的等效字符?

由于

3 个答案:

答案 0 :(得分:5)

您可以编写一系列正则表达式,如:

s = regexprep(s,'(?:À|Á|Â|Ã|Ä|Å)','A')
s = regexprep(s,'(?:Ì|Í|Î|Ï)','I')
其他重音字符的

等等......(对于大写/小写的情况)

警告:即使是拉丁字母的小子集,也有很多variations


一个更简单的例子:

chars_old = 'ÁÉÍÓÚáéíóú';
chars_new = 'AEIOUaeiou';

str = 'Ámró';
[tf,loc] = ismember(str, chars_old);
str(tf) = chars_new( loc(tf) )

之前的字符串:

>> str
str =
Ámró

后:

>> str
str =
Amro

答案 1 :(得分:5)

以下代码规范化所有变音字符,即ÅÄÖ。

function inputWash {
    param(
        [string]$inputString
    )
    [string]$formD = $inputString.Normalize(
            [System.text.NormalizationForm]::FormD
    )
    $stringBuilder = new-object System.Text.StringBuilder
    for ($i = 0; $i -lt $formD.Length; $i++){
        $unicodeCategory = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($formD[$i])
        $nonSPacingMark = [System.Globalization.UnicodeCategory]::NonSpacingMark
        if($unicodeCategory -ne $nonSPacingMark){
            $stringBuilder.Append($formD[$i]) | out-null
        }
    }
    $string = $stringBuilder.ToString().Normalize([System.text.NormalizationForm]::FormC)
    return $string.toLower()
}
Write-Host inputWash("ÖÄÅÑÜ");

oaanu

如果您不想要该功能,请发送.toLower()

答案 2 :(得分:1)

万一还有人需要...我做了,所以我花了一些时间查找所有最常见的变音符号:

function [clean_s] = removediacritics(s)
%REMOVEDIACRITICS Removes diacritics from text.
%   This function removes many common diacritics from strings, such as
%     á - the acute accent
%     à - the grave accent
%     â - the circumflex accent
%     ü - the diaeresis, or trema, or umlaut
%     ñ - the tilde
%     ç - the cedilla
%     å - the ring, or bolle
%     ø - the slash, or solidus, or virgule

% uppercase
s = regexprep(s,'(?:Á|À|Â|Ã|Ä|Å)','A');
s = regexprep(s,'(?:Æ)','AE');
s = regexprep(s,'(?:ß)','ss');
s = regexprep(s,'(?:Ç)','C');
s = regexprep(s,'(?:Ð)','D');
s = regexprep(s,'(?:É|È|Ê|Ë)','E');
s = regexprep(s,'(?:Í|Ì|Î|Ï)','I');
s = regexprep(s,'(?:Ñ)','N');
s = regexprep(s,'(?:Ó|Ò|Ô|Ö|Õ|Ø)','O');
s = regexprep(s,'(?:Œ)','OE');
s = regexprep(s,'(?:Ú|Ù|Û|Ü)','U');
s = regexprep(s,'(?:Ý|Ÿ)','Y');

% lowercase
s = regexprep(s,'(?:á|à|â|ä|ã|å)','a');
s = regexprep(s,'(?:æ)','ae');
s = regexprep(s,'(?:ç)','c');
s = regexprep(s,'(?:ð)','d');
s = regexprep(s,'(?:é|è|ê|ë)','e');
s = regexprep(s,'(?:í|ì|î|ï)','i');
s = regexprep(s,'(?:ñ)','n');
s = regexprep(s,'(?:ó|ò|ô|ö|õ|ø)','o');
s = regexprep(s,'(?:œ)','oe');
s = regexprep(s,'(?:ú|ù|ü|û)','u');
s = regexprep(s,'(?:ý|ÿ)','y');

% return cleaned string
clean_s = s;
end

感谢Amro提供的简单解决方案!