【发布时间】:2012-08-17 06:31:32
【问题描述】:
需要复合表达式
" from" such that " from" is not within parenthesis
(忽略括号中的)这里a=" from"; b="("; and c=")";
我能写的最接近(但无效)的模式是
string pat = @"^((?!\(.* from.*\)).)* from((?!\(.* from.*\)).)*$";
我的表达否认括号中是否存在任何“来自”,但我想严格忽略这样的“来自”
应在以下位置找到匹配项:
1: " from" 2:select field1 from t1 (select field1 from t1) ---- 1 time in both
3: select field1 from t1 (select field1 from t1)select field1 from t1 ---2 times
不包含匹配项的字符串:(因为我想忽略括号内的“from”)
1: select field1 no_f_rom_OutOf_Parenthesis t1 (select field1 from t1)
2: (select field1 from t1) 3: "" (Empty String) 4. No word as form
0 times in all four strings
相关资料:(没必要阅读)
最接近我的问题的最有用的链接告诉我如何匹配“模式”而不是“常规”是 stanav 在 2009 年 7 月 31 日上午 8:05 在以下链接中的回复...
http://www.vbforums.com/archive/index.php/t-578417.html
还有:Regex in C# that contains "this" but not "that
还有:Regular expression to match a line that doesn't contain a word?
我已经学习/搜索了大约一周,但对我来说仍然很复杂:)
【问题讨论】:
-
括号可以嵌套吗?
-
是的,即使它们可以嵌套。但我只需要找到不在任何括号中的“来自”。如果找到了。表达式匹配
-
对于 SQL,您需要一个解析器。对于“hello bro!Whats up”,您也许可以使用正则表达式。
-
我不知道 .NET 的正则表达式是否可以处理嵌套模式,Perl 可以是“递归的”AFAIK。
-
@HenkHolterman。是的,表达式是可变的
标签: c# regex pattern-matching