字符串如下
8-35;9-49;10-65;19-136;21-142;22-145;29-148;158-592;
输入8 获取 35
输入9获取 49
输入8 获取35,592了 这样是错误的
如果 8-35;8-36;9-49;10-65;19-136;21-142;22-145;29-148;158-592;
输入8 获取35,36
string value =string.Empty; string pattern =string.Format("{0}-(?<key>[0-9]*);", propertyID); MatchCollection matchs = Regex.Matches(propertyValue, pattern); if (matchs !=null) { foreach (Match match in matchs) { if (match !=null) { value += match.Groups["key"].Value +","; } } } // 移除最后一个,号if (value.Length >0) value = Common.StringHelper.Remove(value, ",");
string propertyValue = "8-35;8-36;9-49;10-65;19-136;21-142;22-145;29-148;158-592;"; string value = string.Empty; int propertyID = 8; string pattern = string.Format(@"(?<=^|\s+?|\D+?){0}-(?<key>[0-9]*);", propertyID); string _result = string.Join(",",Regex.Matches(propertyValue, pattern).Cast<Match>().Select(a=>a.Groups["key"].Value)); //35,56