Simple UI for Testing RegExps

Share your advanced PureBasic knowledge/code with the community.
User avatar
kenmo
Addict
Addict
Posts: 2043
Joined: Tue Dec 23, 2003 3:54 am

Simple UI for Testing RegExps

Post by kenmo »

I've started messing with PB's Regular Expression library, and to test them out quickly, I whipped up this simple tool.

Enter your regex and test input (plus choose from the four built-in flags) and the resulting matches are shown as you type.

Code: Select all

; +--------------+-------+
; | Regex Tester | kenmo |
; +--------------+-------+
; | http://www.purebasic.fr/english/viewtopic.php?f=12&t=42839

; Create Window

OpenWindow(0, 0, 0, 410, 230, "Regex Tester", #PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)

TextGadget(0, 15, 13, 60, 17, "Regex:", #PB_Text_Center)
  StringGadget(1, 85, 10, 200, 20, "[a-zA-Z]+")
TextGadget(2, 15, 43, 60, 17, "Test Text:", #PB_Text_Center)
  EditorGadget(3, 85, 40, 200, 80)
    SetGadgetText(3, "Hello World")
TextGadget(4, 15, 143, 60, 17, "Matches:", #PB_Text_Center)
  TextGadget(5, 15, 160, 60, 17, "", #PB_Text_Center)
  EditorGadget(6, 85, 140, 200, 80, #PB_Editor_ReadOnly)
    SetGadgetColor(6, #PB_Gadget_BackColor, $D0D0D0)

CheckBoxGadget( 7, 300,  40, 100, 20, "Dot All")
CheckBoxGadget( 8, 300,  60, 100, 20, "Extended")
CheckBoxGadget( 9, 300,  80, 100, 20, "MultiLine")
CheckBoxGadget(10, 300, 100, 100, 20, "Any NewLine")

ButtonGadget(11, 300, 145, 100, 30, "Sticky Window", #PB_Button_Toggle)
  SetGadgetState(11, #True)
ButtonGadget(12, 300, 185, 100, 30, "Close", #PB_Button_Default)

StickyWindow(0, #True)
SetActiveGadget(1)

; Regex Test Procedures

Procedure TestIt()
  Static Filter.s, Input.s, Regex.i, Matches.i, Flags.i, i.i
  Static Dim Match.s(0)
  
  SetGadgetText(5, "")
  SetGadgetText(6, "")
    SetGadgetColor(6, #PB_Gadget_BackColor, $D0D0D0)
  
  Filter = GetGadgetText(1)
  If (Filter)
    Input = GetGadgetText(3)
    Flags = (GetGadgetState( 7) * #PB_RegularExpression_DotAll)
    Flags | (GetGadgetState( 8) * #PB_RegularExpression_Extended)
    Flags | (GetGadgetState( 9) * #PB_RegularExpression_MultiLine)
    Flags | (GetGadgetState(10) * #PB_RegularExpression_AnyNewLine)
    
    Regex = CreateRegularExpression(#PB_Any, Filter, Flags)
    If (Regex)
      Matches = ExtractRegularExpression(Regex, Input, Match())
      If (Matches > 0)
        SetGadgetText(5, Str(Matches))
        SetGadgetColor(6, #PB_Gadget_BackColor, $D0FFD0)
        For i = 0 To Matches - 1
          AddGadgetItem(6, -1, Match(i))
        Next i
      Else
        SetGadgetText(5, "none")
        SetGadgetColor(6, #PB_Gadget_BackColor, $D0D0FF)
      EndIf
      FreeRegularExpression(Regex)
    EndIf
  EndIf
EndProcedure

; Program Loop

TestIt()
Exit = #False

Repeat
  Event = WaitWindowEvent()
  
  If (Event = #PB_Event_CloseWindow)
    Exit = #True
  ElseIf (Event = #PB_Event_Gadget)
    ID = EventGadget()
    Select ID
      Case 1
        If (EventType() = #PB_EventType_Change)
          TestIt()
        EndIf
      Case 3
        If (EventType() = 1024)
          TestIt()
        EndIf
      Case 7, 8, 9, 10
        TestIt()
      Case 11 : StickyWindow(0, GetGadgetState(11))
      Case 12 : Exit = #True
    EndSelect
  EndIf
  
Until Exit
End
Note - I'm checking for EventType() = 1024 to catch EditorGadget keystrokes. This is not an official PB event type, may only work on Windows, etc. so feel free to comment that part out.
KGB_X
User
User
Posts: 63
Joined: Tue Apr 22, 2008 6:06 pm
Location: No(r)way

Re: Simple UI for Testing RegExps

Post by KGB_X »

Seems to work ok in Ubuntu.

So for your next update :wink: let the user select parts of the string and let the program suggest a regex

I have been using: http://www.regular-expressions.info/regexbuddy.html and http://www.regular-expressions.info/regexmagic.html working fine in WINE.

I'm not an regexp nerd but i do see how powerful it is and i'm using it in a few projects. But creating the regexp is for me time consuming.

anyhow nice tool.
Post Reply