IchBistTod Posted March 23, 2010 Share Posted March 23, 2010 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]) Next EndIf 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. [center][/center][center]=][u][/u][/center][center][/center] Link to comment Share on other sites More sharing options...
Fulano Posted March 23, 2010 Share Posted March 23, 2010 (edited) 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")" EndIfnote: I didn't debug this, so there might be some errors in there, but it should be enough to get you started. Edited March 23, 2010 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! Link to comment Share on other sites More sharing options...
IchBistTod Posted March 23, 2010 Author Share Posted March 23, 2010 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. [center][/center][center]=][u][/u][/center][center][/center] Link to comment Share on other sites More sharing options...
Fulano Posted March 23, 2010 Share Posted March 23, 2010 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! Link to comment Share on other sites More sharing options...
jchd Posted March 24, 2010 Share Posted March 24, 2010 (edited) 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 _ArrayDisplay($conditional) ;~ for $x=1 to $conditional[0] <<------- Wrong, no count here! ;~ MsgBox(64, "", $conditional[$x]) ;~ Next EndIfThis doesn't deal with possible comments, and isn't fail-proof:$str = 'if $start > 0 then $string = "So then you believe it works?"'Oops!Making the subpattern less greedy won't help much:$str = 'if $string = "OK. So then you really believe it works?" then call("firemen")'re-Oops!Doing the general case, with possible then embedded in strings, comments, can't be done wihthout a lexer, I suppose. Edited March 24, 2010 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 24, 2010 Moderators Share Posted March 24, 2010 IchBistTod,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]:expandcollapse popup#include <Array.au3> $sText = 'if $start > 0 and $start < 2 then run("calc")' $aArray = _If_Parse($sText) _ArrayDisplay($aArray) $sText = 'if $start > 0 then run("calc")' $aArray = _If_Parse($sText) _ArrayDisplay($aArray) $sText = 'if Not($start > 0) and $start < 2 or $fred = 99 then run("calc")' $aArray = _If_Parse($sText) _ArrayDisplay($aArray) 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) Next ; 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" EndSwitch Next Return $aResult EndFunc ;==>_If_ParseAs you can see, it works on single, double and triple conditions (including those with NOTs). Perhaps you might find it useful. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now