正则式问题

whxbb 2002-04-24 11:23:42
假设有个sql语句的条件字符串
(name='abc') and (id>123) or (memo like '%(v)%')

如何从字符串中提取以下三个片断?
1 (name='abc')
2 and (id>123)
3 or (memo like '%(v)%')
...全文
27 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2002-04-24
  • 打赏
  • 举报
回复
也许不是能让人满意的答案:

string str = "(name='abc') and (id>123) or (memo like '%(v)%')";
string[] sList = System.Text.RegularExpressions.Regex.Split(str,@"\) ");

for (int i=0; i < sList.Length; i++)
{
if (!sList[i].EndsWith(")"))
sList[i] +=")";
Console.WriteLine("{0}:{1}", i, sList[i]);
}
shanminmin 2002-04-24
  • 打赏
  • 举报
回复
这样行不行,你判断")"字符的位置,而后各自取值,当然这样需要所有的条件都用()饱含起来
whxbb 2002-04-24
  • 打赏
  • 举报
回复
十万分的感谢。
saucer 2002-04-24
  • 打赏
  • 举报
回复
试试

using System;
using System.Text.RegularExpressions;

string str = "(name='abc') and (id>123) or (memo like '%(v)%')";
Regex re = new Regex(@"\s*((\w+\s*)?\([^\(\)]+(\([^\)]*\))*[^\)]*\))");
MatchCollection mc = re.Matches(str);
for (int i=0; i < mc.Count; i++)
{
Console.WriteLine("{0}:***{1}***",i, mc[i].Result("$1"));
}
whxbb 2002-04-24
  • 打赏
  • 举报
回复
'%(v) %'就无效了

110,539

社区成员

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

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

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