Jump to content

Regular Expressions for Dummies?


Recommended Posts

While the title is probably paradoxical, I would like help in understanding regular expressions. There's an underlying logic to it that isn't expressed well in the help file, and I'm struggling with learning how it's used. I have several questions:

Is there an existing "Idiot's Guide to RegEx" somewhere on the forums? My searches are turning up a whole lot of helpful topics, but they deal with specific issues, and not learning how to use them.Examples are great, but don't address the syntax or general usage.

Is there a better way of learning RE than reading (and re-reading(and re-re-reading)) the pcrepattern page?

It's all sorts of complistercated.

Thanks!

The PCRE page needs some editing, unless it really is the "pcrepattern man page", in which case, I will do my best to keep the wimmins away.

Link to comment
Share on other sites

  • Moderators

While the title is probably paradoxical, I would like help in understanding regular expressions. There's an underlying logic to it that isn't expressed well in the help file, and I'm struggling with learning how it's used. I have several questions:

Is there an existing "Idiot's Guide to RegEx" somewhere on the forums? My searches are turning up a whole lot of helpful topics, but they deal with specific issues, and not learning how to use them.Examples are great, but don't address the syntax or general usage.

Is there a better way of learning RE than reading (and re-reading(and re-re-reading)) the pcrepattern page?

It's all sorts of complistercated.

Thanks!

The PCRE page needs some editing, unless it really is the "pcrepattern man page", in which case, I will do my best to keep the wimmins away.

I never even noticed that page before lol...

All I can say is ... you need to start with some specific string tests that you think you could run into.

How I learned is when someone posted a question about string manipulation, I'd wait until all the StringLeft/StringTrimLeft/StringRight/StringTrimRight/StringMid/StringLen/While Wend/Do Until/For Next and 50 more lines of code for each question, and try to simply figure their problems out with it.

Obviously I answer a lot of questions, but I think my real boom came when I started learning RegEx... people thought I was helping them, but in reality, they were helping me ... as it would make me study the expressions over and over until I could produce something fluent with them on a consistent basis.

There's plenty of online tutorials you can find through google, but nothing beats putting all other coding down for a week or so, and just test yourself on every scenario you can think of and that pops up on the forums :P.

It's also nice to see the different ways that others come up with their expressions and compare them to yours.

<huff huff> Ok, so the best learning guide? ... IMO, "Hands on", because there are really too many possible string scenarios to cover in any one tutorial.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'd have to agree with Smoke_N and say the only true way to learn is to Fiddle. Alot.

You could you that "Expresso" program to fiddle. But i actually made this a long time ago for myself.. makes fiddling a little easier.

#include <GUIConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>

$GUI = GUICreate("RegEx Tester", 622, 450)
$Group1 = GUICtrlCreateGroup("Input Parameters", 16, 16, 569, 185)
    $String = GUICtrlCreateEdit("", 88, 48, 465, 97)
    $Test = GUICtrlCreateButton("Test", 464, 160, 89, 25, 0)
    $Pattern = GUICtrlCreateInput("", 96, 160, 137, 21)
    $Flag = GUICtrlCreateInput("", 320, 160, 73, 21, BitOR($ES_AUTOHSCROLL,$ES_NUMBER))
    GUICtrlCreateLabel("String:", 32, 88, 34, 17)
    GUICtrlCreateLabel("Pattern:", 32, 160, 41, 17)
    GUICtrlCreateLabel("Flag", 272, 162, 24, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Output", 16, 216, 577, 217)
    $Group3 = GUICtrlCreateGroup("Return Values", 328, 240, 249, 129)
        GUICtrlCreateLabel("Return: ", 344, 264, 42, 17)
        GUICtrlCreateLabel("@Extended", 344, 296, 60, 17)
        GUICtrlCreateLabel("@error", 344, 328, 36, 17)
        $Return = GUICtrlCreateInput("", 408, 264, 137, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
        $Extended = GUICtrlCreateInput("", 408, 296, 137, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
        $Error = GUICtrlCreateInput("", 408, 328, 137, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $ListView = GUICtrlCreateListView("Element|Data", 40, 240, 257, 177, -1, BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT))
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Test
            If GuiCtrlRead($String) = "" or GUICtrlRead($Pattern) = "" Then
                MsgBox(0,"Error", "Please fill in all required parameters.")
            Else
                If GUICtrlRead($Flag) = "" then 
                    Process(GUICtrlRead($String), GuiCtrlRead($Pattern))
                Else
                    Process(GUICtrlRead($String), GuiCtrlRead($Pattern), GUICtrlRead($Flag))
                EndIf
            EndIf
    EndSwitch
WEnd

Func Process($String, $Pattern, $Flag=0)
    _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView))
    $Result = StringRegExp($String,$Pattern,$Flag)
    $Err = @error
    $Ext = @extended
    If IsArray($Result) then
        Dim $Item[UBound($Result)]
        For $x = 0 to UBound($Result)-1
            $Item[$x] = GUICtrlCreateListViewItem("["&$x&"]|"&$Result[$x],$ListView)
        Next
        GUICtrlSetData($Return, "Array")
        GUICtrlSetData($Extended, $Ext)
        GUICtrlSetData($Error, $Err)
    Else
        GUICtrlSetData($Return, $Result)
        GUICtrlSetData($Extended, $Ext)
        GUICtrlSetData($Error, $Err)
    EndIf
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...