1

の複合式が必要です

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

(括弧内は無視)こちらa=" from"; b="("; and c=")";

私が書くことができる最も近い(しかし無効な)パターンは

string pat = @"^((?!\(.* from.*\)).)* from((?!\(.* 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

一致を含まない文字列:(括弧内の「から」を無視したいため)

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




関連資料: (あまり読む必要はありません)

「パターン」に一致するが「通常」には一致しない方法を説明する私の質問に最も近いリンクは、次のリンクの 2009 年 7 月 31 日午前 8 時 5 分に stanav からの返信です...

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

また、「this」を含むが「that」を含まない C# の正規表現

また:単語を含まない行に一致する正規表現?

私は約1週間勉強/検索しましたが、それでも私にとっては複雑です:)

4

2 に答える 2

2

以下は、任意にネストされた括弧を使用しても機能するはずです。

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)"))
于 2012-08-17T11:17:14.737 に答える
1

これはあなたが望むものかもしれません。

string s="select field1 dfd t1 (select field1 from t1)select field1 from t1";
Regex r=new Regex(@"(?<=\)|^)\bselect\b.*?\bfrom\b.*?(?=\()",RegexOptions.RightToLeft);
r.Replace(s,"HELL yeah");
于 2012-08-17T11:10:32.037 に答える