正则表达式替换为重音字符 - AutoHotKey

时间:2014-10-22 19:39:03

标签: regex autohotkey

我在使用带有重音词的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

2 个答案:

答案 0 :(得分:2)

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

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

示例:

<强> http://regex101.com/r/wD3wI1/1

答案 1 :(得分:0)

我设法让它发挥作用:

^+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