在 Embarcadero 的 C++ Builder 中使用 RegEx 将文本拆分为单个单词
Using RegEx to split up a text into single words in Embarcadero's C++ Builder
我正在使用 Embarcadero 的 C++ Builder 开发一个拼写检查程序。我使用正则表达式将文本拆分为单个单词。下面的代码在 RAD Studio XE 上运行良好,但在 RAD Studio Seattle 上的表现不同。
当单词包含非拉丁字符(例如德语变音符号 (Ä,Ö,Ü) 或带有重音符号的字符 (é,ê,à))时,就会出现此问题。
"\w" 被解释为 [a-zA-Z_0-9] 忽略非拉丁字符。
首先,我的上下文中的单词是什么?
可能的单词包括:
"\r\n"
"word-word-word-word ..."
"word." 或 "word-"
带撇号的单词:“'单词”"wor'd""word' "
"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
.
如果您还需要数字(与 \w
一样),请将 \d
添加到组中。
我正在使用 Embarcadero 的 C++ Builder 开发一个拼写检查程序。我使用正则表达式将文本拆分为单个单词。下面的代码在 RAD Studio XE 上运行良好,但在 RAD Studio Seattle 上的表现不同。
当单词包含非拉丁字符(例如德语变音符号 (Ä,Ö,Ü) 或带有重音符号的字符 (é,ê,à))时,就会出现此问题。 "\w" 被解释为 [a-zA-Z_0-9] 忽略非拉丁字符。
首先,我的上下文中的单词是什么? 可能的单词包括:
"\r\n"
"word-word-word-word ..."
"word." 或 "word-"
带撇号的单词:“'单词”"wor'd""word' "
"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
.
如果您还需要数字(与 \w
一样),请将 \d
添加到组中。