正则表达式求一个不在括号内的元素

maifine2 2010-05-11 05:36:30
例如字符串"(where)where(where)" 我想获得中间的where(确保where不在括号内)正则改怎么写,谢谢!
...全文
489 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
maifine2 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 kissxyl 的回复:]
看来csdn下载那个不行

http://www.cnblogs.com/maxchan/articles/861363.html
http://www.cnblogs.com/newsea/archive/2007/10/17/928047.html

伴水的sql分析类。
[/Quote]
我记下了,真的很感谢你,我这个主要是用于写一个高效分页函数用的,因为要支持sqlserver2000才搞的这么麻烦。
KissXYL 2010-05-12
  • 打赏
  • 举报
回复
看来csdn下载那个不行

http://www.cnblogs.com/maxchan/articles/861363.html
http://www.cnblogs.com/newsea/archive/2007/10/17/928047.html

伴水的sql分析类。
maifine2 2010-05-12
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 lxcnn 的回复:]
直接说你的最终目的就是了,何必那么拐弯抹角
看下是否满足你的需求

C# code

string test = "select top 10 * from (select * from omai_user where id>10) where islock=1 order by id desc";
Regex reg = new Regex(@"(?is)(?<!\([^()]*)f……
[/Quote]
非常谢谢你
-过客- 2010-05-12
  • 打赏
  • 举报
回复
直接说你的最终目的就是了,何必那么拐弯抹角
看下是否满足你的需求

string test = "select top 10 * from (select * from omai_user where id>10) where islock=1 order by id desc";
Regex reg = new Regex(@"(?is)(?<!\([^()]*)from\s+(\([^()]*\)|[^()])+where");
Match m = reg.Match(test);
if(m.Success)
{
richTextBox2.Text = m.Value + "\n";
}
/*-------输出-------
from (select * from omai_user where id>10) where
*/
maifine2 2010-05-12
  • 打赏
  • 举报
回复
终于解决了,用平衡分组能解决,谢谢各位了,我把分给牛兄了,虽然大家都很牛。
Match match = Regex.Match("((aaa where bbb) aaa where xxxx) where asda", @"((?<open>\()|(?<-open>\))|[^()])*(?<tt>where)(?(open)(?!))");
if (match.Success)
{
Response.Write(match.Groups["tt"].Index);
}
sxldfang 2010-05-12
  • 打赏
  • 举报
回复
虽已结贴,但作为技术性讨论,再跟一贴,根据楼主 15 楼的描述,特设置如下测试字符串:
aa where bb(a where b(aa where bb )a where b)a where (where)where

对应的正则表达式,都可正确找到find的where
(?sxn)(\(((?<p>\()*|(?<-p>\))*|[^()]*)*(?(p)(?!))\))*([^()]*)(?<find>where)

但为什么把前面的改成 逆序环视匹配,就把所有的where 都找到了呢?如下:
(?sxn)(?<=(\(((?<p>\()*|(?<-p>\))*|[^()]*)*(?(p)(?!))\))*)([^()]*)(?<find>where)

大家再分析分析!!!
maifine2 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 kissxyl 的回复:]
csdn下载搜索到一个可能更简单方法。
SQL语句生成及分析器
[/Quote]
牛兄,平衡分组你也试了没用吗?
KissXYL 2010-05-11
  • 打赏
  • 举报
回复
csdn下载搜索到一个可能更简单方法。
SQL语句生成及分析器
maifine2 2010-05-11
  • 打赏
  • 举报
回复
我获得后截取from到这个位置的字符串

Match match = Regex.Match("((aaa where bbb) aaa where xxxx) where", @"((?<open>\()*|(?<-open>\))*|[^()]*)where(?(open)(?!))");
if (match.Success)
{
Response.Write(match.Index);
}
我这样写的出来的不知道为什么是2
-过客- 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 maifine2 的回复:]
我要获得第三个where的位置
[/Quote]

获取位置后你想做什么?
maifine2 2010-05-11
  • 打赏
  • 举报
回复
Match match = Regex.Match("((aaa where bbb) aaa where xxxx) where", @"?!(\(((?<Open>\()|(?<-Open>\))|[^()])*(?(Open)(?!))\))*?\s{0,}where\s{0,}*?");
if (match.Success)
{
Response.Write(match.Index);
}
出错:parsing "?!(\(((?<Open>\()|(?<-Open>\))|[^()])*(?(Open)(?!))\))*?\s{0,}where\s{0,}*?" - Quantifier {x,y} following nothing.
我要获得第三个where的位置
KissXYL 2010-05-11
  • 打赏
  • 举报
回复
不太了解,客客,lambda慢么?
xk1126 2010-05-11
  • 打赏
  • 举报
回复
Regex reg = new Regex(@"?!(\(((?<Open>\()|(?<-Open>\))|[^()])*(?(Open)(?!))\))*?\s{0,}where\s{0,}*?");

-过客- 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 maifine2 的回复:]
真的非常感谢 我也发现一条正则判断真的很难实现
其实就是想把这个模块用于复杂SQL语句的分析和提取。
比如:select top 10 * from (select * from omai_user where id>10) where islock=1 order by id desc
分别提取top,cols,tablename等等,呵呵看来只有用函数实现啦。
[/Quote]

楼主的需求描述不清楚,不知道你究竟想做什么,最终想做的仅仅是提取?固定内容提取没有什么意义,替换倒还可以考虑
只要你的规则明确,正则还是可以实现的
至于是不是一条正则实现,没有什么实际意义,一条正则实现通常可读性会很差,而且未必有拆分多个正则匹配效率高

PS:建议正则不要与Lambda表达式一起使用,这样更会给那些不懂正则应用场景,只知道怪正则效率低的人以口实
sxldfang 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 wangkun9999 的回复:]
答案是一条正则当然可以解决,我在家里没法测试,随手写一条:

C# code

Regex reg = new Regex(@"?!(\(((?<Open>\()|(?<-Open>\))|[^()])*(?(Open)(?!))\))*?\s{0,}where\s{0,}*?");
[/Quote]
这里有个正则表达式测试工具,你下载一个试试吧,我也想看看结果!
http://61.163.246.20/root/RegularExpression.rar
wangkun9999 2010-05-11
  • 打赏
  • 举报
回复
答案是一条正则当然可以解决,我在家里没法测试,随手写一条:

Regex reg = new Regex(@"?!(\(((?<Open>\()|(?<-Open>\))|[^()])*(?(Open)(?!))\))*?\s{0,}where\s{0,}*?");
KissXYL 2010-05-11
  • 打赏
  • 举报
回复
不过你实际要实现的功能你没有说明白。不好说如何实现。总之,可能只能如我所说的方法,几个正则嵌套使用,外加一点语句。
KissXYL 2010-05-11
  • 打赏
  • 举报
回复
我给你的你测试过么?可以提取你要的内容啊。
maifine2 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 kissxyl 的回复:]
简化一下。省掉一层正则。

C# code

private void TestRegex09()
{
List<string> history = new List<string>();
string yourStr = "(((where)where(where))) where";
string result = Regex.Replace(
……
[/Quote]
真的非常感谢 我也发现一条正则判断真的很难实现
其实就是想把这个模块用于复杂SQL语句的分析和提取。
比如:select top 10 * from (select * from omai_user where id>10) where islock=1 order by id desc
分别提取top,cols,tablename等等,呵呵看来只有用函数实现啦。
谢谢大家。
wangkun9999 2010-05-11
  • 打赏
  • 举报
回复
这个属于复杂的正则表达式中的平衡组,参考:
http://blog.csdn.net/lxcnn/archive/2009/08/03/4402808.aspx

我就懒得写了,过客兄来解答吧
加载更多回复(17)

110,537

社区成员

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

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

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