public const byte wildcard = 0x3F; // 通配符 /// <summary> /// 模糊匹配所有 /// </summary> /// <param name="content">被查找对象</param> /// <param name="pattern">查找串</param> /// <returns></returns> public static int[] MatchAll(byte[] content, byte[] pattern) { byte[] head = GetHead(pattern); int[] indexs = BoyerMooreMatcher.MatchAll(content, head); // 头串和查找串相同则直接返回,不同则继续判断是否符合查询串 if (head.Length == pattern.Length) { return(indexs); } else { List <int> res = new List <int>(); foreach (int index in indexs) { if (IsEqual(content, index, pattern)) { res.Add(index); } } if (res.Count > 0) { return(res.ToArray()); } else { return(null); } } }
/// <summary> /// 通配符匹配所有符合查找串的位置,并排除已经替换的情况 /// </summary> /// <param name="content">被查找对象</param> /// <param name="searchBytes">查找串</param> /// <param name="replaceBytes">替换串</param> /// <returns></returns> public static int[] MatchNotReplaced(byte[] content, byte[] searchBytes, byte[] replaceBytes) { byte[] head = GetHead(searchBytes); int[] indexs = BoyerMooreMatcher.MatchAll(content, head); // 头串和查找串相同则直接返回,不同则继续判断是否符合查询串 List <int> res = new List <int>(); if (head.Length != searchBytes.Length) { foreach (int index in indexs) { if (IsEqual(content, index, searchBytes)) { res.Add(index); } } indexs = res.ToArray(); } // 判断是否与替换串相同 res = new List <int>(); foreach (int index in indexs) { if (!IsEqual(content, index, replaceBytes)) { res.Add(index); } } return(res.ToArray()); }