Browse Source

修正匹配html的img标签的bug

懒得勤快 8 years ago
parent
commit
5c1256b028
3 changed files with 28 additions and 17 deletions
  1. 22 8
      Masuit.Tools/Html/HtmlHelper.cs
  2. 2 2
      Masuit.Tools/Net/WebExtension.cs
  3. 4 7
      Test/Program.cs

+ 22 - 8
Masuit.Tools/Html/HtmlHelper.cs

@@ -434,18 +434,29 @@ namespace Masuit.Tools.Html
 
 
         /// <summary>
-        /// 匹配html的img标签
+        /// 匹配html的所有img标签集合
         /// </summary>
         /// <param name="html"></param>
         /// <returns></returns>
-        public static Match MatchImgTag(this string html) => Regex.Match(html, @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>");
+        public static MatchCollection MatchImgTags(this string html) => Regex.Matches(html, @"<img[\s]+src[\s]*=[\s]*((['""](?<src>[^'""]*)[\'""])|(?<src>[^\s]*))");
+
+        /// <summary>
+        /// 匹配html的一个img标签
+        /// </summary>
+        /// <param name="html"></param>
+        /// <returns></returns>
+        public static Match MatchImgTag(this string html) => Regex.Match(html, @"<img[\s]+src[\s]*=[\s]*((['""](?<src>[^'""]*)[\'""])|(?<src>[^\s]*))");
 
         /// <summary>
         /// 获取html中第一个img标签的src
         /// </summary>
         /// <param name="html"></param>
         /// <returns></returns>
-        public static string MatchFirstImgSrc(this string html) => MatchImgTag(html).Groups[1].Value;
+        public static string MatchFirstImgSrc(this string html)
+        {
+            MatchCollection collection = MatchImgTags(html);
+            return collection.Count > 0 ? collection[0].Groups["src"].Value : String.Empty;
+        }
 
         /// <summary>
         /// 随机获取html代码中的img标签的src属性
@@ -454,13 +465,16 @@ namespace Masuit.Tools.Html
         /// <returns></returns>
         public static string MatchRandomImgSrc(this string html)
         {
-            GroupCollection groups = MatchImgTag(html).Groups;
-            string img = groups[new Random().StrictNext(groups.Count)].Value;
-            if (img.StartsWith("<"))
+            MatchCollection collection = MatchImgTags(html);
+            if (collection.Count > 0)
             {
-                return img.MatchImgTag().Groups[1].Value;
+                string img = collection[new Random().StrictNext(collection.Count)].Value;
+                if (img.StartsWith("<"))
+                {
+                    return img.MatchImgTag().Groups["src"].Value;
+                }
             }
-            return img;
+            return String.Empty;
         }
 
         /// <summary>

+ 2 - 2
Masuit.Tools/Net/WebExtension.cs

@@ -282,9 +282,9 @@ namespace Masuit.Tools.Net
                     PhysicsAddress address = JsonConvert.DeserializeObject<PhysicsAddress>(result);
                     if (address.Status == 0)
                     {
-                        string detail = $"{address.AddressResult.FormattedAddress} {address.AddressResult.AddressComponent.Direction}{address.AddressResult.AddressComponent.Distance}米";
+                        string detail = $"{address.AddressResult.FormattedAddress} {address.AddressResult.AddressComponent.Direction}{address.AddressResult.AddressComponent.Distance ?? "0"}米";
                         List<string> pois = new List<string>();
-                        address.AddressResult.Pois.ForEach(p => { pois.Add($"{p.AddressDetail}{p.Name} {p.Direction}{p.Distance}米"); });
+                        address.AddressResult.Pois.ForEach(p => { pois.Add($"{p.AddressDetail}{p.Name} {p.Direction}{p.Distance ?? "0"}米"); });
                         return new Tuple<string, List<string>>(detail, pois);
                     }
                 }

+ 4 - 7
Test/Program.cs

@@ -1,10 +1,5 @@
 using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Text;
-using System.Xml.Serialization;
-using Masuit.Tools;
-using Masuit.Tools.Win32;
+using Masuit.Tools.Html;
 
 namespace Test
 {
@@ -45,7 +40,9 @@ namespace Test
             //"114.114.256.114".MatchInetAddress(out isIP);//False
             //"114.114.114.114".MatchInetAddress(out isIP);//True
             //Console.WriteLine(isIP);
-            Console.WriteLine(WindowsCommand.Execute("help"));
+            //Console.WriteLine(WindowsCommand.Execute("help"));
+            string match = "vawevbgw".MatchRandomImgSrc();
+            Console.WriteLine(match);
             Console.ReadKey();
         }
     }