首页 新闻 会员 周边

求正则 在线等立即结贴

0
悬赏园豆:120 [已解决问题] 解决于 2012-09-01 17:49

字符串如下
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, ",");
bcacyy的主页 bcacyy | 初学一级 | 园豆:40
提问于:2012-09-01 17:44
< >
分享
最佳答案
1
 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
收获园豆:120
暗尘掩月 | 初学一级 |园豆:183 | 2012-09-01 17:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册