Jump to content
Sign in to follow this  

help with regex

Recommended Posts


okay i have the following code

if StringRegExp($data[$i], "(?i)(.*?)if(.*?)then(.*?)") Then
     $conditional = StringRegExp($data[$i], "(?i)(.*?)if(.*?)then(.*?)", 1)
     for $x=1 to $conditional[0]
     MsgBox(64, "", $conditional[$x])

I am trying to extract "$start > 0 and $start < 2" and 'run("calc")' from 'if $start > 0 and $start < 2 then run("calc")'

the condition and action. But the regex returns 0.

Can someone tell me what is wrong? I am fairly new to regex.


Share this post

Link to post
Share on other sites

Well, if the string is always going to be "if <something> and <something> then ..." this should work:

Local $conditional =  StringRegExp($data[$i], "(?i)if\s*(.*)\s*then\s*(.*)", 1)
If not @error then
    ; $conditional[0] will hold "$start > 0 and $start < 2" from your example string
    ; $conditional[1] will hold "run("calc")"
note: I didn't debug this, so there might be some errors in there, but it should be enough to get you started.

Edited by Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post

Link to post
Share on other sites

It wont if you looked im attempting to capture the condition and action out of a conditional statement.

it will always be "if <something>then<something>"

conditions can contain any boolean operator, not, =, <, >, and, or.


Share this post

Link to post
Share on other sites

Sorry about that, I had some trouble reading your post. I'll edit my previous to correct that.

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post

Link to post
Share on other sites

Try this:

Local $str = '  elseif  $start > 0 and $start < 2 then  run("calc")'
$conditional = StringRegExp($str, "(?i)\A\s*(?:else)?if\s+(.+)\s+then\s+(.+)\z", 1)
if Not @error Then
;~  for $x=1 to $conditional[0]     <<------- Wrong, no count here!
;~  MsgBox(64, "", $conditional[$x])
;~  Next

This doesn't deal with possible comments, and isn't fail-proof:

$str = 'if $start > 0 then $string = "So then you believe it works?"'


Making the subpattern less greedy won't help much:

$str = 'if $string = "OK. So then you really believe it works?" then call("firemen")'


Doing the general case, with possible then embedded in strings, comments, can't be done wihthout a lexer, I suppose.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post

Link to post
Share on other sites


Does it have to be a RegEx? I ask because this little function seems to do what you want. :(

It splits the action from the conditon(s) and determines any logical relationship between multiple conditions - all presented in a nice little array with a condition counter at [0][0]:

#include <Array.au3>

$sText = 'if $start > 0 and $start < 2 then run("calc")'

$aArray = _If_Parse($sText)

$sText = 'if $start > 0 then run("calc")'

$aArray = _If_Parse($sText)

$sText = 'if Not($start > 0) and $start < 2 or $fred = 99 then run("calc")'

$aArray = _If_Parse($sText)

Func _If_Parse($sText)

    ; Separate conditions and action
    $aSplit_1 = StringSplit(StringUpper($sText), "THEN", 1)

    ; Split any conditions and remove WS
    $aSplit_2 = StringSplit(StringRegExpReplace(StringStripWS(StringTrimLeft($aSplit_1[1], 2), 3), "AND|OR", "#####"), "#####", 1)
    For $i = 1 To $aSplit_2[0]
        $aSplit_2[$i] = StringStripWS($aSplit_2[$i], 3)

    ; Declare result array and enter number of conditions and action
    Local $aResult[$aSplit_2[0] + 1][2] = [[$aSplit_2[0], StringStripWS($aSplit_1[2], 3)]]

    ; Detect logical operators and add them and conditions to result array
    For $i = 1 To $aSplit_2[0]
        $aResult[$i][1] = $aSplit_2[$i]
        Switch StringLeft(StringStripWS(StringMid($aSplit_1[1], StringInStr($aSplit_1[1], $aSplit_2[$i]) + StringLen($aSplit_2[$i])), 3), 1)
            Case "A"
                $aResult[$i + 1][0] = "AND"
            Case "O"
                $aResult[$i + 1][0] = "OR"

    Return $aResult

EndFunc   ;==>_If_Parse

As you can see, it works on single, double and triple conditions (including those with NOTs). Perhaps you might find it useful. :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
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
Sign in to follow this