2011-03-24 128 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