Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Regular Expression Tester


  • Please log in to reply
14 replies to this topic
rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
This script allows for the testing of regular expressions for both RegExMatch() and RegExReplace(). The results update in real time and any setting or expression errors are highlighted in red.

Uses RegExMatch's "object" mode so is AHK_L only.

Comments and constructive criticism welcome. grin.png
Spoiler

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Wow, man! This will be really useful to save time creating Regex.
Thanks a lot, rbrtryn!!!

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Hi, rbrtryn! Can you make it support the m option?
For example, I'm using this needle m)000$ to replace the last 3 0's in every line, but if I use multiple lines the $ will match only the last one. I know I could test in one of the lines then use it in a script with a multline variable, but it would be interesting if I could see the results in different lines with different values.

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

Hi, rbrtryn! Can you make it support the m option?
For example, I'm using this needle m)000$ to replace the last 3 0's in every line, but if I use multiple lines the $ will match only the last one. I know I could test in one of the lines then use it in a script with a multline variable, but it would be interesting if I could see the results in different lines with different values.

RegExReplace() doesn't need or use the "m" option. It always scans the entire string, newlines included. The problem is your expression is incorrect for what you want it to do. Use 000(\n|$) for your needle, and back-reference to return the newlines. Example: NewStr := RegExReplace(Haystack, "000(\n|$)", "444$1")

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Well, your example works alright, but the "m" option worked for me in this case too.
Anyway, thanks for showing this code, it seems to be more appropriate. I don't know much about Regular Expressions yet and I'm trying to understand it better! :)

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
might want to mention in the topic that this is for AHK_L only

gargoyle888
  • Members
  • 27 posts
  • Last active: Nov 10 2015 03:56 PM
  • Joined: 10 Jan 2009
Nice work, Thanks

Sjc1000
  • Members
  • 572 posts
  • Last active: Mar 11 2017 11:41 AM
  • Joined: 06 Feb 2012
Ooooh, nice job.. This will be very helpful. THANKS

Sjc1000 - Insert inspirational quote here!

PLEASE find me on the IRC if you have questions. I'm never on the forum anymore.

 


  • Guests
  • Last active:
  • Joined: --
As it is not working with ahk basic it should be posted under custom scripts.

Can some moderator move it ? Thank you

Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010
Thank you for sharing your script, rbrtryn, it's very useful. I was just about to start writing a similar script and then I thought: "Surely someone has written this already", and here it is. Your script is already saving me time and I will be using it regularly (no pun intended).

RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus


jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
Text to be searched: yahoo weather api

RegEx Tester
(?<=temperature=\")(.*)(?=\"\s*distance)     ===> works      => C
(?<=temperature="")(.*)(?=""\s*distance)     ===> dont work
AHK-Script
(?<=temperature=\")(.*)(?=\"\s*distance)     ===> dont work
(?<=temperature="")(.*)(?=""\s*distance)     ===> works      => C

[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

noname
  • Members
  • 650 posts
  • Last active:
  • Joined: 12 Nov 2011

Thanks jNizM for bringing this utility to the foreground :)

Very nice rbrtryn i am sure i will use it a lot. :shy:

 

 

Added autofocus on the editcontrol for convenience (or frustration to others.........)

/*
    Regex Tester - A front end for testing Perl Compatible Regular Expressions.
                   The results update in realtime and any setting or expression
                   errors are highlighted in red.
                   
                   Alt+C will copy the currently displayed expression to the Clipboard.
    
    Version 1.0
    Last Update 5-29-2012
    By Robert Ryan
    
*/


;    Customized for my use noname :Added mousetracking to put autofocus on editcontrols




; AutoeExecute
    #NoEnv
    #SingleInstance force

    gosub MakeGui
    gosub UpdateMatch
    gosub UpdateReplace
    Gui Show, , Regex Tester
    OnMessage(0x200,"OnMouseMove") 
return

#IfWinActive Regex Tester
!c::
    Gui Submit, NoHide
    ClipBoard := (TabSelection = "RegExMatch") ? mNeedle : rNeedle
    MsgBox, 64, RegEx Copied, %Clipboard% has been copied to the Clipboard, 3
return

GuiEscape:
GuiClose:
    ExitApp
return


OnMouseMove( wParam, lParam, Msg ) { 
      if A_GuiControl 
      GuiControl,focus,%a_guicontrol%
      }

; This is called any time any of the edit boxes on the RegExMatch tab are changed.
UpdateMatch:
    Gui Submit, NoHide
    
    if not IsInteger(mStartPos) {
        mStartPos := 1
        Gui Font, cRed 
        GuiControl Font, mStartPos
    }
    else {
        Gui Font, cDefault
        GuiControl Font, mStartPos
    }
    
    ; Set Needle to return an object
    mNeedle := RegExReplace(mNeedle, "^(\w*)\)", "O$1)", cnt)
    if (! cnt) {
        mNeedle := "O)" mNeedle
    }
    
    FoundPos := RegExMatch(mHaystack, mNeedle, Match, mStartPos)
    if (ErrorLevel) {
        Gui Font, cRed 
        GuiControl Font, mNeedle
    }
    else {
        Gui Font, cDefault
        GuiControl Font, mNeedle
    }
    
    ResultText := "FoundPos: " FoundPos "`n"
    ResultText .= "Match: " Match.Value() "`n"
    Loop % Match.Count() {
        ResultText .= "Match["
        ResultText .= (Match.Name[A_Index] = "") 
                    ? A_Index 
                    :  Match.Name[A_Index] 
        ResultText .= "]: " Match[A_Index] "`n"
    }
    GuiControl, , mResult, %ResultText%
return

; This is called any time any of the edit boxes on the RegExReplace tab are changed.
UpdateReplace:
    Gui Submit, NoHide
    
    if not IsInteger(rStartPos) {
        rStartPos := 1
        Gui Font, cRed 
        GuiControl Font, rStartPos
    }
    else {
        Gui Font, cDefault
        GuiControl Font, rStartPos
    }
    
    if not IsInteger(rLimit) {
        rLimit := -1
        Gui Font, cRed 
        GuiControl Font, rLimit
    }
    else {
        Gui Font, cDefault
        GuiControl Font, rLimit
    }
    
    NewStr := RegExReplace(rHaystack, rNeedle, rReplacement, rCount, rLimit, rStartPos)
    if (ErrorLevel) {
        Gui Font, cRed 
        GuiControl Font, rNeedle
    }
    else {
        Gui Font, cDefault
        GuiControl Font, rNeedle
    }
    
    ResultText := "Count: " rCount "`n"
    ResultText .= "NewStr: `n" NewStr "`n"
    
    GuiControl, , rResult, %ResultText%
return

MakeGui:
    Gui Font, s10, Consolas
    Gui Add, Tab2, r25 w400 vTabSelection, RegexMatch|RegexReplace
    
    Gui Tab, RegexMatch
        Gui Add, Text, , Text to be searched:
        Gui Add, Edit, r12 w370 vmHaystack gUpdateMatch
        Gui Add, Text, Section, Regular Expression:
        Gui Add, Edit, r2 w275 vmNeedle gUpdateMatch
        Gui Add, Text, x+15 ys, Start: (1)
        Gui Add, Edit, r1 w75 vmStartPos gUpdateMatch, 1
        Gui Add, Text, xs, Results:
        Gui Add, Edit, r14 w370 +readonly -TabStop vmResult
        
    Gui Tab, RegexReplace
        Gui Add, Text, , Text to be searched:
        Gui Add, Edit, r10 w370 vrHaystack gUpdateReplace
        Gui Add, Text, w275 Section, Regular Expression:
        Gui Add, Edit, r2 w275 vrNeedle gUpdateReplace
        Gui Add, Text, , Replacement Text:
        Gui Add, Edit, r2 w275 vrReplacement gUpdatereplace
        Gui Add, Text, , Results:
        Gui Add, Edit, r12 w370 +readonly -TabStop vrResult
        Gui Add, Text, ys xs+290 Section, Start: (1)
        Gui Add, Edit, r1 w75 vrStartPos gUpdateReplace, 1
        Gui Add, Text, xs y+35 , Limit: (-1)
        Gui Add, Edit, r1 w75 vrLimit gUpdateReplace, -1
return

IsInteger(str) {
    if str is integer
        return true
    else
        return false
}

winXP  and ahk unicode


jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
np noname =)
but it would be nice if someone can say why this is?
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011
**REMOVE**

Jack Dunning
  • Members
  • 217 posts
  • Last active: Nov 11 2015 08:40 PM
  • Joined: 08 Apr 2013

np noname =)
but it would be nice if someone can say why this is?

I'm guessing that it's because this tester was written for Perl RegEx and not AutoHotkey RegEx. Double quote escape character is different for the two as shown in your example.


I currently do a regular blog for AutoHotkey beginners and have posted a number of AutoHotkey help pages at ComputorEdge.com. As I learn, I pass it on.

 

AutoHotkey scripts and apps for beginners and more ideas.