gpt4 book ai didi

c++ - 在 Embarcadero 的 C++ Builder 中使用 RegEx 将文本拆分为单个单词

转载 作者:行者123 更新时间:2023-11-28 05:32:07 25 4
gpt4 key购买 nike

我正在使用 Embarcadero 的 C++ Builder 开发一个拼写检查器应用程序。我使用正则表达式将文本拆分为单个单词。下面的代码在 RAD Studio XE 上运行良好,但在 RAD Studio Seattle 上表现不佳。

当单词包含非拉丁字符(如德语变音符号 (Ä,Ö,Ü) 或带有重音符号的字符 (é,ê,à))时,就会出现问题。"\w"被解释为 [a-zA-Z_0-9],忽略非拉丁字符。

首先,在我的语境中什么是词?可能的词包括:

  • "\r\n"

  • “单词-单词-单词-单词...”

  • “单词。”或“单词-”

  • 带撇号的词:“'word”“wor'd”“word'”

  • “单词”

  • 有两种不同类型的撇号:' 和 ’

代码如下:

String text (L"Österreich l'année);
const String sRegex (L"\r\n|(\\w+\\-)+\\w+|\\w+(\\.|\\-)|('|’)?\\w+('|’)?\\w*");
TRegEx regex(sRegex, TRegExOptions());
TMatchCollection regexMatches = regex.Matches(text);
for (int i=0; i<regexMatches.Count; ++i)
{
TMatch regexMatch = regexMatches.Item[i];
String word (regexMatch.Value);

//do stuff with word
}

字符串单词的所需值为“Österreich”和“l'année”。而RegEx匹配的是“sterreich”、“l'ann”和“e”。

我的问题是,如何指定所有非拉丁字符?

最佳答案

\p{L} 匹配一个 unicode 字母。尝试使用它代替 \w

See it here at regex101 .

如果您还需要数字(与 \w 一样),请将 \d 添加到组中。

关于c++ - 在 Embarcadero 的 C++ Builder 中使用 RegEx 将文本拆分为单个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39224709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com