查找匹配“a”的模式,忽略位于“b”和“c”内的“a”

2023-12-02

需要一个复合表达式

" 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 日上午 08:05 在以下链接中的回复...

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

Also: C# 中的正则表达式包含“this”但不包含“that”

Also: 正则表达式匹配不包含单词的行?

我已经研究/搜索了大约一周,但对我来说仍然很复杂:)


即使使用任意嵌套的括号,以下内容也应该有效:

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))

作为单行正则表达式(“恐怖!恐怖!”),如果你坚持:

if (Regex.IsMatch(subjectString,@"\sfrom(?=(?:[^()]*\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\))*[^()]*\z)"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查找匹配“a”的模式,忽略位于“b”和“c”内的“a” 的相关文章

随机推荐