【问题标题】:Regex replace with accented characters - AutoHotKey正则表达式替换为重音字符 - AutoHotKey
【发布时间】:2014-10-23 05:43:18
【问题描述】:

我在使用带有重音符号的 regex.replace 时遇到了问题 - 我正在使用 regex replace 因为我需要替换完整而不是部分字符串(请参阅列表中的“grund”)。

但 AHK 似乎忽略了单词开头和结尾的重音字符(中间没问题)。有没有人遇到过这个问题?

我想出了以下“修复”,通过在尾随重音字符之后在前导重音字符之前添加下划线,但是当单词中出现重音时它仍然无法正常工作(请参阅'mémit' 和 mèmit' )。有人可以帮忙吗?我确信有一种更简单的方法来处理口音!

干杯!

^+f2::
data =
(
testé = WORD1
kragén = WORD2
und = WORD3
gürtel = WORD4
émail = WORD5
élder = WORD7
messé = WORD8
émit = WORD9
èmit = WORD10
testè = WORD11
)
text =
(
testé kragén und gürtel émail nomâtch élder messé émit émit èmiter émita mémit mèmit testé testè grund
)


text := RegExReplace(text,"(\w+é)\W|$","$1_ ")
text := RegExReplace(text,"\W(é\w+)"," _$1")
text := RegExReplace(text,"(\w+è)\W|$","$1_ ")
text := RegExReplace(text,"\W(è\w+)"," _$1")


loop, parse, data, `n, `r
{
stringsplit, term, a_loopfield, =, %a_space%
text := RegExReplace(text, "\b" . term1 . "\b", term2)
}


stringreplace, text, text,  _, , all
stringreplace, text, text, _ , , all


msgbox, % text
return

【问题讨论】:

    标签: regex autohotkey


    【解决方案1】:

    您应该能够使用\p{L} 匹配Unicode 字符:

    ([\p{L}\w]+)
    

    示例:

    http://regex101.com/r/wD3wI1/1

    【讨论】:

    • 谢谢,但它用于查找和替换脚本,所以我需要每个单词的精确匹配,而不是包罗万象的正则表达式代码。
    • 对于单个字符,例如é,您可能会使用\x(hex code of character) - ([\xE9]) 等。
    【解决方案2】:

    我设法让它工作:

    ^+f2::
    data =
    (
    testé = WORD1
    kragén = WORD2
    und = WORD3
    gürtel = WORD4
    émail = WORD5
    élder = WORD7
    messé = WORD8
    émit = WORD9
    èmit = WORD10
    testè = WORD11
    )
    
    data := RegExReplace(data, "è", "00egrave")
    data := RegExReplace(data, "é", "00eacute")
    data := RegExReplace(data, "ü", "00uuml")
    
    text =
    (
    testé kragén und gürtel émail nomâtch élder messé émit émit èmiter émita mémit mèmit testé testè grund
    )
    
    text := RegExReplace(text, "é", "00eacute")
    text := RegExReplace(text, "ê", "00egrave")
    text := RegExReplace(text, "ü", "00uuml")
    
    loop, parse, data, `n, `r
    {
    stringsplit, term, a_loopfield, =, %a_space%
    text := RegExReplace(text, "\b" . term1 . "\b", term2)
    }
    
    text := RegExReplace(text, "00egrave", "è")
    text := RegExReplace(text, "00eacute", "é")
    text := RegExReplace(text, "00uuml", "ü")
    
    msgbox, % text
    return
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2013-10-25
      • 1970-01-01
      • 2015-11-30
      • 2017-02-04
      相关资源
      最近更新 更多