查找匹配模式'A',忽略'一'说谎'B'和'C'内 [英] Find a pattern to match 'a', ignoring that 'a' which lies within 'b' and 'c'

查看:129
本文介绍了查找匹配模式'A',忽略'一'说谎'B'和'C'内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一个复合表达式

" from" such that " from" is not within parenthesis

(忽略那些在括号中)这里 A =从 ; B =(;和c =);

(ignoring those which are in parenthesis) here a=" from"; b="("; and c=")";

最近的(但无效)模式,我可以写的

string pat = @"^((?!\(.* from.*\)).)* from((?!\(.* from.*\)).)*$";



我的表情否认如果有从出现在括号中,但我想严格忽略这种从

my expression denies if any " from" is present in parenthesis but i want to strictly ignore such " 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

字符串不包含匹配:(因为我想忽略从括号内)

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

搜索结果搜索

有关材料:(没有太大的必要读)

Relevant Material: (not much necessary to read)

最有用的链接更接近我的问题,告诉如何匹配'模式',但不是'正规军'已经在7月31日通过stanav回复2009年,上午08时05分在下面的链接...

The most helpful link nearer to my question telling how to match 'pattern' but not 'regular' has been a reply by stanav at Jul 31st, 2009, 08:05 AM in following link...

http://www.vbforums.com/archive/index.php/t-578417.html

另外:正则表达式在C#中包含"本"但不与QUOT;这

另外:的正则表达式匹配不包含字串?

我研究/搜索约一个星期,但还是它对于我来说复杂的:)

I have studied/searched about a week but still Its complex for me:)

推荐答案

下面应该工作,即使任意嵌套的括号:

The following should work, even with arbitrarily nested parentheses:

if (Regex.IsMatch(subjectString, 
    @"\sfrom           # Match ' from'
    (?=                # only if the following regex can be matched here:
     (?:               # The following group, consisting of
      [^()]*           # any number of characters except parentheses,
      \(               # followed by an opening (
      (?>              # Now match...
       [^()]+          #  one or more characters except parentheses
      |                # or
       \( (?<DEPTH>)   #  a (, increasing the depth counter
      |                # or
       \) (?<-DEPTH>)  #  a ), decreasing the depth counter
      )*               # any number of times
      (?(DEPTH)(?!))   # until the depth counter is zero again,
      \)               # then match the closing )
     )*                # Repeat this any number of times.
     [^()]*            # Then match any number of characters except ()
     \z                # until the end of the string.
    )                  # End of lookahead.", 
    RegexOptions.IgnorePatternWhitespace))



作为一个单行的正则表达式,如果你坚持(以下简称恐怖恐怖!):

As a single line regex ("The horror! The horror!"), if you insist:

if (Regex.IsMatch(subjectString,@"\sfrom(?=(?:[^()]*\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\))*[^()]*\z)"))

这篇关于查找匹配模式'A',忽略'一'说谎'B'和'C'内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆