2

我将如何使用 Regex 匹配 Unicode 字符串?我正在从一个文本文件中加载几个关键字,并将它们与 Regex 一起用于另一个文件。关键字都包含 unicode(例如á,等)。我不确定问题出在哪里。我必须设置一些选项吗?


代码:

foreach (string currWord in _keywordList)
{
    MatchCollection mCount = Regex.Matches(
        nSearch.InnerHtml, "\\b" + @currWord + "\\b", RegexOptions.IgnoreCase);

    if (mCount.Count > 0)
    {
        wordFound.Add(currWord);
        MessageBox.Show(@currWord, mCount.ToString());
    }
}

并将关键字读取到列表中:

var rdComp = new StreamReader(opnDiag.FileName);
string compSplit = rdComp.ReadToEnd()
                         .Replace("\r\n", "\n")
                         .Replace("\n\r", "\n");
rdComp.Dispose();
string[] compList = compSplit.Split(new[] {'\n'});

然后我将数组更改为列表。

4

2 回答 2

1

在匹配特定字符时,我相信正则表达式仅支持 ASCII 字符集的文字。除此之外,您可以使用 \uxxxx 来匹配 Unicode 代码点。

这里

于 2010-03-29T13:56:34.423 回答
0

您可以使用 [\u0000-\uffff]+ 至少匹配 BMP

于 2014-11-18T16:35:34.993 回答