1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Text.RegularExpressions;6 usingSystem.Threading.Tasks;7
8 namespaceResolveHtmlText9 {10 classProgram11 {12 static void Main(string[] args)13 {14 string text = @" y 1 23 4 5ggjf123456";15 Console.WriteLine("原字符串:" +text);16 text = text.Replace("\"", "'");17 text = text.Replace(""", "'");18 text = text.Replace(" ", "");19 text = text.Replace("<", "
20 text = text.Replace(">", ">");//將>的轉義碼>都替換成>21
22 //string matchStr = @"[^]*";
23 string matchStr = @"
24 + @"+\s*:\s*[^];?'\s*>[^]"
25 + @"*|
26 + @"+\s*(\s*[a-zA-Z-]+\s*=\s*'(\s*[a-zA-Z-]+\s*:"
27 + @"\s*[^:^;^]+\s*;\s*)*(\s*[a-zA-Z-]+\s*:\s*"
28 + @"[^:^;^]+\s*)\s*;?\s*'\s*)*"
29 + @"\s*>[^]*";30
31 Regex htmlReg = newRegex(matchStr);32 string result = null;33 MatchCollection htmlMatchCollection =htmlReg.Matches(text);34 StringBuilder sb = newStringBuilder();35
36 foreach (Match m inhtmlMatchCollection)37 {38 if (m != null && m.Groups != null && m.Groups.Count > 0)39 {40 string temp = m.Groups[0].Value;41 Console.WriteLine("臨時值:" +temp);42 //Regex textReg1 = new Regex(@"[^]+");43 //Match textMatch1 = textReg1.Match(temp);44 //if (textMatch1 != null && textMatch1.Groups != null && textMatch1.Groups.Count > 0)45 //{46 //result = textMatch1.Groups[0].Value;47 //sb.Append(result);48 //}
49 Regex textReg = new Regex(@">.+ 0)52 {53 result = textMatch.Groups[0].Value;54 if (result.Length > 2)55 {56 result = result.Substring(1, result.Length - 2);57 sb.Append(result);58 }59 }60 }61 }62
63
64
65 Console.WriteLine("解析出的結果:" +sb.ToString());66 Console.ReadLine();67 }68 }69 }