1. 程式人生 > 實用技巧 >C# 匹配開始字元和結束字元

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); } }