c# html 后台拼_C#解析單層html的中的文本,然后拼接起來

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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值