Regex Not Matching Unicode

混江龙づ霸主 提交于 2019-12-21 21:36:52

问题


How would I go about using Regex to match Unicode strings? I'm loading in a couple keywords from a text file and using them with Regex on another file. The keywords both contain unicode (such as á, etc). I'm not sure where the problem is. Is there some option I have to set?


Code:

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());
    }
}

And reading the keywords to a list:

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'});

Then I change the array to a list.


回答1:


When matching on a specific character, I believe regular expressions only support literals for the ASCII character set. Beyond that, you can use \uxxxx to match on the Unicode code point.

See here.




回答2:


You can use [\u0000-\uffff]+ to match at least the BMP



来源:https://stackoverflow.com/questions/2538201/regex-not-matching-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!