2012-02-15 61 views
6

我正在使用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. 

對於上述情況的所有情況,我想突出顯示文本框中特定的文本。我正在嘗試使用正則表達式。我將所有的情況都插入到數組列表中並逐個檢查。對於第一種情況,如果文本框中的文本與hjhdf,dfsjf類似,那麼它將顯示消息框,如果在此特定文本之前和之後有任何文本,則它將不顯示消息框。

string regexerror = wordToFind; 
Regex myregex = new Regex("^[,]*[a-zA-Z]*$"); 
bool isexist = myregex.IsMatch(rtbFileDisplay.Text); 
if (isexist) 
{ 
    MessageBox.Show("Hi"); 
} 
+1

您可以使用'(?<=^|>)[^><] +?(?= <| $)\ be(\ w *)s \ b'來匹配html中的文本。 – 2012-02-15 07:21:29

回答

2

此刻你只匹配整個文本的開頭與^。你需要讓它匹配一行的開始。

看看這個鏈接:http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx。這解釋了MultiLine屬性的使用:

myregex.MultLine=true; 

這應該可以完成這項工作。

+0

那麼,它的工作? :-) – deadlyvices 2012-02-17 11:32:48

+0

對於我需要使用Regex.Split()的一些條件;那麼它工作正常 – 2012-02-22 05:20:18

+0

看看一個名爲The Regulator的工具:http://sourceforge.net/projects/regulator/。這將幫助您開發和測試正則表達式。 – deadlyvices 2012-02-22 09:55:54