【问题标题】:Replace special characters by equivalent用等效替换特殊字符
【发布时间】:2012-10-07 18:31:25
【问题描述】:

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

元音:ÁÉÍÓÚáéíóú 分别来自 AEIOUaeiou。和字母Ñ由N。

表达式:

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

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

谢谢

【问题讨论】:

    标签: string matlab replace diacritics


    【解决方案1】:

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

    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
    

    【讨论】:

    • 谢谢@Amro,事实上我只是在使用西班牙语子集,所以特殊字符只是上面显示的。没有更简单的解决方案吗?像 PHP 的 str_replace 之类的东西,您可以在其中将包含等价的数组作为数组参数传递?
    • 另一种可能性是将 Perl(在 MATLAB 中是 available)与 Text::Unidecode 等模块一起使用。这是一个非常强大的解决方案,它可以做一些有趣的事情,比如从 Unicode 音译到 ASCII。它已被移植到其他语言,如 Python、Java、..(我过去使用过 Python 移植)
    • @JorgeZapata:我添加了一个更简单的示例。 chars_old 中的每个字符都替换为 chars_new 中的对应字符。您可以以相同的方式将带有波浪号的 N 添加到列表中
    【解决方案2】:

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

    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()

    【讨论】:

      【解决方案3】:

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

      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 的简单解决方案!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-03
        • 2019-09-23
        • 1970-01-01
        • 1970-01-01
        • 2011-12-28
        相关资源
        最近更新 更多