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

선택 만 강조 표시되므로 문제가 내 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

+2

흠, 첫 번째 선택 부분 만 강조 표시됩니다. 대신'Regex.Matches'를 사용하십시오. 문제 (코드를 테스트하지 않은 경우) 일 수 있습니다. –

+0

또한'\\ b's로 무엇을하려합니까? 정말로 입력 데이터에서 백 스페이스 리터럴을 기대하십니까? – ildjarn

답변

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'입니다. –