Simple RegExp tool
Posted: Tue Jun 08, 2010 11:01 pm
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.)
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)