목록

2009-11-27 6 views
0

에서 선택 특수 문자를 제거하는 방법은 C 번호 목록이 곳목록

<b>Moon</b>

같은 값이 많이 나는 <b></b>을 제거 할.

결과는 다음과 같습니다. Moon.

목록에서이 유형의 문자를 제거하려면 어떻게해야합니까?

+0

귀하의 게시물 서식 코드에 의해 엉망이 된 것으로 나타납니다} <>

using System; using System.Text.RegularExpressions; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var input = "<b>The</b> <i>Man</i> on the <U><B>Moon</B></U>"; var regex = new Regex("<.*?>", RegexOptions.IgnoreCase | RegexOptions.Multiline); var output = regex.Replace(input, string.Empty); Console.WriteLine(output); Console.ReadLine(); } } 

로 빈 브래킷의 경우를 잡으려고 열심히하면 더 빨리 ... – fyjham

답변

5

당신은 XML 태그 제거 XDocument를 사용할 수 있습니다

string StripXmlTags(string xml) 
{ 
    XDocument doc = XDocument.Parse(xml); 
    return doc.Root.Value; 
} 

예 :

[Test] 
public void Test() 
{ 
    string xml = "<root><b>nice </b><c>node</c><d><e> is here</e></d></root>"; 
    string result = StripXmlTags(xml); 

    Assert.AreEqual("nice node is here", result); 
} 
1

이 시도 :

var moonHtml = "<b>Moon</b>"; 
var regex = new Regex("</?(.*)>", RegexOptions.IgnoreCase | RegexOptions.Multiline); 
var moon = regex.Replace(moonHtml, string.Empty); 
+0

12 초 시작하는 것을 알 수 있습니다 , 무슨 수치입니까;) – Elephantik

+1

뒤에 오는 점에 슬래시가 포함되었을 때 "0 또는 1 /"'/? '을 지정하는 이유는 무엇입니까? 왜 알파벳 문자가 없을 때 ignore-case를 지정합니까? 모범 사례? 오 잘. 코드가 욕심이 많습니다. 'abc Moon more text more moon '과 같은 문자열이 있다면 "abc"로 끝납니다. –

+1

정규식을 사용하여 html을 구문 분석하지 마십시오. - http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – thecoop

0

을보십시오이 :

Regex.Replace("<b>Moon</b>", @"\<.+?\>", "") 
0
string noHtml = Regex.Replace(inputWithHtmlTags, "<[^>]+>", ""); 
0

이 프로그램은 모든 태그를 제거하는 정규식 일러스트레이션으로 기울임 꼴과 밑줄을 제거 할만큼 유연합니다. IgnoreCase 옵션을 사용하여 입력에 <b> 또는 <B>이 없도록 보호하고 여러 줄로 검색합니다. 이를 실행하는 결과물은 "The Man on the Moon"입니다. 나는 사용한다 .*? 0 개 이상을 의미하는 것은 같은