2012-07-03 108 views
2

我在我的網站上使用了一個富文本編輯器,我正在使用一個標籤來顯示上次用戶輸入的內容,但是當他提交代碼時,它包含了來自標籤。當我提交表單,寫入下列....刪除部分字符串C#

<span id="bodyContent_header1"> <div style"float:right">The Text On The Page </div> </span> 

我需要刪除從代碼中<span id="bodyContent_header1"> </span>並保持休息。誰能幫忙?此外代碼可能包含其他的span標籤,所以它應該每次只刪除一個。

+0

一些代碼示例肯定會有所幫助。 – J0HN

+0

我想你可以用正則表達式或者使用string.split或者.substring來做到這一點。 – Tomtom

回答

3

鑑於這種字符串是單行你可以使用正則表達式^<span[^>]+>|</span>$更換最<span>標籤像這樣:

string strRegex = @"^<span[^>]+>|</span>$"; 
RegexOptions myRegexOptions = RegexOptions.Multiline | RegexOptions.Singleline; 
Regex myRegex = new Regex(strRegex, myRegexOptions); 
string strTargetString = @"<span id=""bodyContent_header1""> <div style""float:right"">The Text On The Page </div> </span>\n"; 
string strReplace = @""; 
return myRegex.Replace(strTargetString, strReplace); 

這個作品在RegexHero

+0

謝謝,這很好 – mwhite14

1

這個表達式從這個標籤

(?<=<span id="bodyContent_header1">)(.+)(</span>) 

決心選擇數據與一個正則表達式這個任務替換IMPOSIBLE