我正在使用WebBrowser控件处理html文档,我需要做一个实用程序来搜索一个单词,并在浏览器中突出显示它。如果字符串是英语的,它可以很好地工作,但对于其他语言的字符串,例如韩语,它似乎不起作用。
下面提到的代码工作的场景是-
考虑到用户已经在网页中选择了一个单词"Example“,现在我需要突出显示这个单词及其所有出现的地方。我还需要计算它们的byteOffset (代码片段只做这件事)。
现在,对于英语语言,下面的代码运行良好,但对于韩语这样的语言,它根本不起作用。
它不会进入for-each循环
foreach (Match m in reg.Matches(this._documentContent)) 在这里,_documentContent以字符串的形式包含网页源代码。occurenceNo是第一名。所选单词在文档中的出现次数
这是代码,strTemp包含朝鲜语字符串:
string strTemp = myRange.text;
string strExp =@">(([^<])*?)" + strTemp + "(([^<])*?)<";
int intCount =0;
Regex reg = new Regex(strExp);
Regex reg1 = new Regex(strTemp);
foreach (Match m in reg.Matches(this._documentContent))
{
string strMatch = m.Value;
foreach (Match m2 in reg.Matches(strMatch))
{
intCount += 1;
if (intCount==OccurenceNo)
{
int intCharOffset = m.Index + m2.Index;
System.Text.UTF8Encoding d = new System.Text.UTF8Encoding();
int intByteOffset = d.GetBytes( _documentContent.Substring(1, intCharOffset)).Length;
}
}
}发布于 2010-11-17 14:58:28
如果代码对英语单词有效,但对韩语没有返回任何结果,那么我可能会建议这是一个文化问题,所以您可以尝试将RegexOptions设置为CultureInvariant:
Regex reg = new Regex(strExp, RegexOptions.CultureInvariant);
Regex reg1 = new Regex(strTemp, RegexOptions.CultureInvariant);发布于 2014-10-16 02:04:45
我使用以下韩语的RegEx代码:
private static readonly Regex regexKorean = new Regex(@"[가-힣]");
public static bool IsKorean(this char s)
{
return regexKorean.IsMatch(s.ToString());
}
if (someText.Any(z => z.IsKorean()))
{
DoSomething();
}https://stackoverflow.com/questions/1706389
复制相似问题