Page 1 of 1

Simple RegExp tool

Posted: Tue Jun 08, 2010 11:01 pm
by blueznl
Without a doubt someone already posted such a thing, and probably better than this one... oh well...

Just something simple I cooked up to test regular expressions. Type your regexp in the top box, the little green square will show you if it is a valid expression or not.

Type a number of strings (seperated by <cr>) in the bottom box. All matches stay black, all non matches turn red.

The whole thing is realtime, so edit and it shows the results at once.

(Lots of the code related to the editor gadget was grabbed from the forum.)

Code: Select all

EnableExplicit

Global event.i, nn.i, s.s, n.i, x.i, y.i, re_nr.i, re_old.s, text.s, text_old.s, re.s


Procedure x_editorgadget_select(gadgetnr.i,beginrow.i=-2,begincolumn.i=-2,endrow.i=-2,endcolumn.i=-2)  
  Protected range.CHARRANGE, id.i
  ;
  id = GadgetID(gadgetnr)
  ;
  If beginrow = -1                                                     
    beginrow = 0
    begincolumn = 0
  ElseIf beginrow = -2                                                 
    begincolumn = 0
    beginrow = 0
    endrow = -1
    endcolumn = -1
  EndIf
  ;
  If begincolumn = -1                                                  
    begincolumn = 0
  ElseIf begincolumn = -2                                              
    begincolumn = 0
    endrow = beginrow
    endcolumn = -1
  EndIf
  ;
  range\cpMin = SendMessage_(id, #EM_LINEINDEX, beginrow, 0) + begincolumn
  ;
  If endrow = -1                                                       
    endrow = SendMessage_(id, #EM_GETLINECOUNT, 0, 0)-1
    endcolumn = -1
  ElseIf endrow = -2                                                   
    endrow = beginrow
    endcolumn = begincolumn
  EndIf
  ;
  range\cpMax = SendMessage_(id, #EM_LINEINDEX, endrow, 0)
  ;
  If endcolumn = -2                                                    
    endcolumn = begincolumn
  EndIf
  If endcolumn = -1                                                    
    range\cpMax = range\cpMax + SendMessage_(id, #EM_LINELENGTH, range\cpMax, 0)
  Else
    range\cpMax = range\cpMax + endcolumn
  EndIf
  ;
  SendMessage_(id, #EM_EXSETSEL, 0, @range)
  ;
EndProcedure

Procedure x_editorgadget_frontcolor(gadgetnr.i,color.i)                              
  Protected format.CHARFORMAT, id.i
  ;
  id = GadgetID(gadgetnr)
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_COLOR
  format\crTextcolor = color
  SendMessage_(id, #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
  ;
EndProcedure

Procedure x_editorgadget_cursorcolumn(gadgetnr.i)
  Protected range.CHARRANGE, id.i
  ;
  range.CHARRANGE
  id = GadgetID(gadgetnr)
  SendMessage_(id,#EM_EXGETSEL,0,range.CHARRANGE)
  ProcedureReturn (range\cpMax-(SendMessage_(id,#EM_LINEINDEX,SendMessage_(id,#EM_EXLINEFROMCHAR,0,range\cpMin),0)))
  ; 
EndProcedure

Procedure x_editorgadget_cursorrow(gadgetnr)
  Protected range.CHARRANGE, id.i
  ;
  range.CHARRANGE
  id = GadgetID(gadgetnr)
  SendMessage_(id,#EM_EXGETSEL,0,@range)
  ProcedureReturn SendMessage_(id,#EM_EXLINEFROMCHAR,0,range\cpMin)
  ;
EndProcedure

CreateImage(1,9,22)
OpenWindow(1,10,10,400,400,"Regular Expressions",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
TextGadget(1,10,10,370,20,"Pattern")
StringGadget(2,10,30,365,24,"")
ImageGadget(3,380,31,9,22,ImageID(1))
TextGadget(4,10,70,185,20,"Test")
EditorGadget(5,10,90,380,300)
;
Repeat
  event = WaitWindowEvent()
  ;
  Select event
  Case #PB_Event_CloseWindow
  EndSelect
  ;
  text = GetGadgetText(5)
  re = GetGadgetText(2)
  re_nr = CreateRegularExpression(#PB_Any,re)
  If re_nr = 0
    StartDrawing(ImageOutput(1))
      Box(0,0,10,30,RGB($FF,$00,$00))
    StopDrawing()
    SetGadgetState(3,ImageID(1))
  ElseIf re_old <> re Or text_old <> text
    StartDrawing(ImageOutput(1))
      Box(0,0,10,30,RGB($00,$FF,$00))
    StopDrawing()
    SetGadgetState(3,ImageID(1))
    ;
    x = x_editorgadget_cursorcolumn(5)
    y = x_editorgadget_cursorrow(5)
    ;
    n = CountGadgetItems(5)
    For nn = 0 To n-1
      s = GetGadgetItemText(5,nn)
      x_editorgadget_select(5,nn)
      If MatchRegularExpression(re_nr,s)
        x_editorgadget_frontcolor(5,RGB($0,$0,$0))
      Else
        x_editorgadget_frontcolor(5,RGB($FF,$00,$00))
      EndIf
    Next nn
    ;
    x_editorgadget_select(5,y,x)
    ;
    FreeRegularExpression(re_nr)
    re_old = re
    text_old = text
    ;
  EndIf
  ;
Until event = #PB_Event_CloseWindow
;
CloseWindow(1)

Re: Simple RegExp tool

Posted: Sat Jun 12, 2010 5:50 pm
by cas
Thanks, this will be really helpful for learning regular expressions.

There is only one minor problem with editor redraw when there is already lots of lines (vertical scrollbar is displayed) and when i type in new text. I fixed it with #WM_SETREDRAW and #RDW_INVALIDATE so it looks smooth now.

Code: Select all

  ElseIf re_old <> re Or text_old <> text
    StartDrawing(ImageOutput(1))
      Box(0,0,10,30,RGB($00,$FF,$00))
    StopDrawing()
    SetGadgetState(3,ImageID(1))
    SendMessage_(GadgetID(5),#WM_SETREDRAW, #False, #False) ; <==============
    ;
    x = x_editorgadget_cursorcolumn(5)
    y = x_editorgadget_cursorrow(5)
    ;
    n = CountGadgetItems(5)
    For nn = 0 To n-1
      s = GetGadgetItemText(5,nn)
      x_editorgadget_select(5,nn)
      If MatchRegularExpression(re_nr,s)
        x_editorgadget_frontcolor(5,RGB($0,$0,$0))
      Else
        x_editorgadget_frontcolor(5,RGB($FF,$00,$00))
      EndIf
    Next nn
    ;
    x_editorgadget_select(5,y,x)
    ;
    SendMessage_(GadgetID(5),#WM_SETREDRAW, #True, #False) ; <==============
    RedrawWindow_(GadgetID(5),#False,#False,#RDW_INVALIDATE) ; <==============
    FreeRegularExpression(re_nr)
    re_old = re
    text_old = text
    ;
  EndIf

Re: Simple RegExp tool

Posted: Sun Jun 13, 2010 9:39 pm
by blueznl
Yeah, perhaps the best way would be to store each line into an array, and only update those sections that require an update, but I was a bit lazy and it seemed to work fine :-)

Got a better version in the making, but I'm running into a nasty CodeCaddy bug, which blocks me for the moment. Will be continued... some day :-)

Re: Simple RegExp tool

Posted: Sun Sep 12, 2010 9:37 pm
by blueznl