我正在使用c#开发一个Windows应用程序.我正在将文件(html,txt,xhtml)加载到文本框中.我想在我的文本框中测试以下情况的发生.
,(comma) with closeup text (ex. text1,text2)
.(dot) with closeup text (ex. text1.text2)
:(colon) with closeup text (ex. text1:text2)
,(comma) with closeup ' i.e (left single quotation mark)
"(doublequote) with closeup text
'(single quote) with closeup text
</i> with closeup text (ex. </i>text)
</span> with closeup text.
Run Code Online (Sandbox Code Playgroud)
对于上述条件的所有出现,我想突出显示文本框中特定的文本.我正在尝试使用正则表达式.我将所有案例插入数组列表并逐个检查.对于第一种情况,如果文本框中的文本类似于hjhdf,则dfsjf然后它将显示消息框,如果在此特定文本之前和之后有任何文本,则它将不显示消息框.
string regexerror = wordToFind;
Regex myregex = new Regex("^[,]*[a-zA-Z]*$");
bool isexist = myregex.IsMatch(rtbFileDisplay.Text);
if (isexist)
{
MessageBox.Show("Hi");
}
Run Code Online (Sandbox Code Playgroud)
目前,您仅将整个文本的开头与^. 您需要让它匹配行的开头。
查看此链接:http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx。这解释了 MultiLine 属性的使用:
myregex.MultLine=true;
Run Code Online (Sandbox Code Playgroud)
这应该可以完成工作。