求正则 在线等 立即结贴

暗尘掩月 2012-08-25 05:00:51
字符串如下
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
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗尘掩月 2012-08-25
  • 打赏
  • 举报
回复
测试OK了 给分结贴
  • 打赏
  • 举报
回复
 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
暗尘掩月 2012-08-25
  • 打赏
  • 举报
回复

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, ",");

这样会出现 8 获取35,592的情况

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧