在字符串突变中使用正则表达式中的重音字母

usage of accented letters in regex in string mutations

如何修改我的字符串突变正则表达式代码,使其也适用于重音字母? 例如,"amor" 的 reges 中的字符串突变应该与“āmōr”的相同。我试着简单地包含像 ´(?<=[aeiouāēīōūăĕĭŏŭ])´ 这样的重音字母,但这没有用。

我的代码:

$hyphenation = '~
(?<=[aeiou]) #each syllable contain a vowel
(?:
    # Muta cum liquida
    ( (?:[bcdfgpt]r | [bcfgp] l | ph [lr] | [cpt] h | qu ) [aeiou] x )
  |
    [bcdfghlmnp-tx]
    (?:
        # ct goes together

        [cp] \K (?=t)
      |
        # two or more consonants are splitted up
        \K (?= [bcdfghlmnp-tx]+ [aeiou]) 
    )   
  |
    # a consonant and a vowel go together
    (?:
        \K (?= [bcdfghlmnp-t] [aeiou])
      | 
        #  "x" goes to the preceding vowel
        x \K (?= [a-z] | (*SKIP)(*F) ) 
    )
  |
    # two vowels are splitted up except ae oe...
    \K (?= [aeiou] (?<! ae | oe | au | que | qua | quo | qui ) ) 
)
~xi';


// hyphention
$result = preg_replace($hyphenation, '-', $input);

在 Unicode 中可以用多种方式表示一个重音字母。例如ā可以是unicode码位U+0101(带有MACRON的拉丁文小写字母A),也可以是U+0061(拉丁文小写字母A)和U+0304(COMBINING MACRON)的组合. (link)

结果,写作 (?<=[aeiouāēīōūăĕĭŏŭ]) 是正确的,如果:

  • 您使用 u 修饰符通知 pcre 正则表达式引擎您的字符串和模式必须被读取为 UTF-8 字符串。否则多字节字符将被视为分隔的字节而不是原子(这可能会产生问题并产生奇怪的结果,特别是当多字节字符位于字符 class 内时。例如 [eā]+ 将匹配“ē ").

  • 您确定目标字符串和模式对每个字母使用相同的形式。如果模式使用 U+0101 和字符串 U+0061 以及 U+0304 作为“ā”,它将不起作用。为防止出现此问题,您可以将 $str = Normalizer::normalize($str); 应用于主题字符串。此方法来自 intl 扩展。

您可以通过以下链接找到更多信息:

https://en.wikipedia.org/wiki/Unicode_equivalence
http://utf8-chartable.de/
http://php.net/manual/en/normalizer.normalize.php
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
http://pcre.org/original/pcre.txt