2011-03-24 59 views
0
 let m = Regex.Match(X.Text, "\\b(select)|(where)|(from)\\b", RegexOptions.IgnoreCase) 

它只突出顯示Select,所以我猜這個麻煩是在我的Regex.Match語法中,但是我看不到在哪裏?正則表達式匹配其中一個字符串

與alll我目前的解決辦法是尋找這樣的變化:

module SQL_Highlighing 

open System.Runtime.InteropServices 

module Lock = 
    [<DllImport(@"User32", CharSet = CharSet.Ansi, SetLastError = false, ExactSpelling = true)>] 
    extern void LockWindowUpdate(int hWnd) 

open System.Text.RegularExpressions 
open System.Drawing 

type SyntaxRTB() = 
    inherit System.Windows.Forms.RichTextBox() 

    override X.OnTextChanged(e : System.EventArgs) = 
     base.OnTextChanged(e); X.ColorTheKeyWords() 

    member X.ColorTheKeyWords() = 
     let HL s c = 
      let color(m : Match, color : Color) = 
       X.SelectionStart <- m.Index 
       X.SelectionLength <- m.Length 
       X.SelectionColor <- color 
      Regex.Matches(X.Text, "\\b" + s + "\\b", RegexOptions.IgnoreCase) |> fun mx -> 
       for m in mx do if (m.Success) then color(m,c) 

     let SelectionAt = X.SelectionStart 
     Lock.LockWindowUpdate(X.Handle.ToInt32()) 

     HL "(select)|(where)|(from)|(top)|(order)|(group)|(by)|(as)|(null)" Color.Blue 
     HL "(join)|(left)|(inner)|(outer)|(right)|(on)" Color.Red 
     HL "(and)|(or)|(not)" Color.DarkSlateGray 
     HL "(case)|(when)|(then)|(else)|(end)" Color.BurlyWood 
     HL "(cast)|(nvarchar)|(bit)" Color.BlueViolet 
     HL "(datepart)" Color.Teal 

     X.SelectionStart <- SelectionAt 
     X.SelectionLength <- 0 
     X.SelectionColor <- Color.Black 
    Lock.LockWindowUpdate(0) 
+0

我懷疑它與你的問題有關,但你的P/Invoke簽名是錯誤的。它應該是'extern bool LockWindowUpdate(nativeint hWnd)'和'SetLastError'應該設置爲'false'。 – ildjarn 2011-03-24 11:04:51

+2

嗯,它只會突出第一個選擇,從哪裏或從哪裏。改爲使用'Regex.Matches'。這可能是問題(尚未測試您的代碼)。 – 2011-03-24 11:08:30

+0

另外,你想用'\\ b's完成什麼?你真的希望在輸入數據中使用退格文字嗎? – ildjarn 2011-03-24 11:08:34

回答

4

從評論遷移

Regex.Match會只給你第一場比賽。相反,你應該使用Regex.Matches

5

我建議使用正則表達式的測試牀。我發現GSkinner RegExr非常有用。

\ b代表一個邊界,但|正在分離你的表情。您實際得到的是:

\b(select) 
or 
(where) 
or 
(from)\b 

我假設你要爲每一個邊界,所以加入另一組將阻止分離:

\b((select)|(from)|(where))\b 
+2

+1這是另一個問題:)簡化將是'\ b(select | from | where)\ b',因爲'|'的優先級最低。 – 2011-03-24 11:17:35

相關問題