C# 匹配开始字符和结束字符
/// <summary> /// 获得字符串中开始和结束字符串 /// </summary> /// <param name="str">字符串</param> /// <param name="s">开始</param> /// <param name="e">结束</param> /// <returns></returns> public static List<string> GetValue(string str, string s, string e) { return Regex.Matches(str, "[.\\s\\S]*?(?<=(" + s + "))[.\\s\\S]*?(?<=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline) .Cast<Match>() .Select(t => t.Value.LastIndexOf(s) == -1 ? t.Value : t.Value.Substring(t.Value.LastIndexOf(s))).ToList(); }
private void GetAllDataStr(string DataValue) { try { string regx = "(?<=(01))[.\\s\\S]*?(?=(04))"; if (string.IsNullOrWhiteSpace(DataValue)) return; bool isMatch = Regex.IsMatch(DataValue, regx); if (!isMatch) return; MatchCollection matchCol = Regex.Matches(DataValue, regx); string[] result = new string[matchCol.Count]; if (matchCol.Count > 0) { for (int i = 0; i < matchCol.Count; i++) { result[i] = "01" + matchCol[i].Value + "04"; StrToASCII(result[i]); } } } catch(Exception ex) { WriteLog.Write(ex.Message); } }